要求
请编写函数fun,其功能是:编写函数char *fun(char*s0,char *s1,char *s2,char*s3),
要求实现:
将s0所指字符串分解成三个字符串,分别存入s1、s2、s3所指内存中。分解的方法是,s1、s2、53从s0中依次顺序每隔3个字符取1例如:s0为"abcdefghij"时,分解完成后,s1、s2、S3分别为:"adgj""cfi”"beh"
解题思路
通过三次循环将s0的字符分别放在s1,s2,s3中
代码
#include <stdio.h>
#include <string.h>
void fun(char * s0,char *s1,char *s2, char *s3)
{int i = 0;int j = 0;int k = 0;int l = 0;for(i=0;i<strlen(s0);i=i+3){s1[j++]=s0[i];}for(i=1;i<strlen(s0);i=i+3){s2[k++]=s0[i];}for(i=2;i<strlen(s0);i=i+3){s3[l++]=s0[i];}s1[j]='\0';s2[k]='\0';s3[l]='\0';
}
void main()
{void NONO();char s0[100],s1[40],s2[40],s3[40];printf("请输入一行字符串\n");gets(s0);fun(s0,s1,s2,s3);puts(s1);puts(s2);puts(s3);NONO();
}
void NONO()
{FILE *fp = fopen("out.dat", "w");char s0[10][100]={"1234567890qazwsx","abcdefghij","0987654321plmokn","fsdjfsdlrj564342dsf","gfdklgjdsfl4754398","zxcvbnmasdfg","asdfghjkl123","qwertyuiop456","qweasdzxc789","poiuytrewqwsxqaz"};char s1[40],s2[40],s3[40];int i;for(i=0;i<10;i++){fun(s0[i],s1,s2,s3);fprintf(fp,"s1=%s,s2=%s,s3=%s\n", s1,s2,s3);}fclose(fp);
}