Example: In the following given program, the function of the function fun is to copy all the characters in the string p to the string a, and insert a space after every three characters copied.
例如,在调用fun函数之前给字符串s输入asdf,调用函数之后,字符串a中的内容则为asd f。
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构。
代码如下:
#include<stdio.h>
void fun(char*p,char*a)
{
int i,j=0;
while(*p)
{
i=0;
while(i<3&&*p)
{
a[j]=*p;
j++;
p++;
i++;
}
if(*p)
a[j++]=' ';
}
a[j]='\0';
}
main()
{
char s[80],a[80];
printf("Enter a string: ");
gets(s);
printf("The original string: ");
puts(s);
fun(s,a);
printf("\nThe string after insertspace: ");
puts(a);
printf("\n\n");
}
The output running window is as follows:
越努力越幸运!
加油,奥力给!!!