Example: Please write the function fun. The function of this function is to combine the M strings (the length of each string does not exceed N) in the string array into a new string in order.
例如,若字符串数组中的M个字符串为:
1 1 1 1
2 2 2 2 2 2 2
3 3
4 4 4 4
则合并后的字符串的内容应是11112222222334444
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
代码如下:
#include<stdio.h>
#define M 4
#define N 20
void fun(char str[M][N],char*a)
{
int i,j,k=0;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
if(*(*(str+i)+j))
{
a[k]=*(*(str+i)+j);
k++;
}
else
break;
a[k]='\0';
}
}
main()
{
char matrix[M][N]={
"1111","2222222","33","4444"},i;
char str[100]={
"****************"};
FILE*out;
printf("The string:\n");
for(i=0;i<M;i++)
puts(matrix[i]);
printf("\n");
fun(matrix,str);
printf("The string:\n");
printf("%s",str);
printf("\n\n");
out=fopen("outfile.dat","w");
fprintf(out,"%s",str);
fclose(out);
}
The output running window is as follows:
越努力越幸运!
加油,奥力给!!!