C language programming> Week 15 ③ Please write the function fun. The function of this function is to put the character data in a two-dimensional array of M rows and N columns into a string in the order of columns.

Example: Please write the function fun. The function of this function is to put the character data in a two-dimensional array of M rows and N columns into a string in the order of columns.

例如:若二维数组中的数据为:
* * * *
@ @ @ @
# # # #
则字符串中的内容应是:
*@#*@#*@#*@#
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

代码如下:

#include<stdio.h>
#define M 3
#define N 4
void fun(char(*a)[N],char*c)
{
    
    
	int i,j,k=0;
	for(j=0;j<N;j++)
		for(i=0;i<M;i++)
			{
    
    
			c[k]=*(*(a+i)+j);
			k++;
		}
	c[k]='\0';
}
main()
{
    
    
	char s[100],str[M][N]={
    
    {
    
    '*','*','*','*'},{
    
    '@','@','@','@'},{
    
    '#','#','#','#'}};
	int i,j;
	FILE*out;
	printf("The matrix:\n");
	for(i=0;i<M;i++)
	{
    
    
		for(j=0;j<N;j++)
			printf("%3c",str[i][j]);
		printf("\n");
	}
	fun(str,s);
	printf("The A string:");
	puts(s);
	printf("\n\n");
	out=fopen("outfile.dat","w");
	fprintf(out,"%s",s);
	fclose(out);
}

The output running window is as follows:
Insert picture description here

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

Guess you like

Origin blog.csdn.net/qq_45385706/article/details/112061176