4

C语言编程>第十三周 ② 请编写函数fun,该函数的功能是:将放在字符串数组中的M个...

 3 years ago
source link: https://blog.csdn.net/qq_45385706/article/details/111994123
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C语言编程>第十三周 ② 请编写函数fun,该函数的功能是:将放在字符串数组中的M个字符串(每串的长度不超过N),按顺序合并组成一个新的字符串。

original.png
朽木自雕i 2021-01-05 09:21:57 articleReadEyes.png 476
分类专栏: C语言程序设计

例题:请编写函数fun,该函数的功能是:将放在字符串数组中的M个字符串(每串的长度不超过N),按顺序合并组成一个新的字符串。

例如,若字符串数组中的M个字符串为:
1 1 1 1
2 2 2 2 2 2 2
3 3
4 4 4 4
则合并后的字符串的内容应是11112 2 2 2 2 2 23 34 4 4 4
请勿改动主函数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);
}

输出运行窗口如下:
在这里插入图片描述

越努力越幸运!
加油,奥力给!!!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK