C语言编程>第十一周 ⑤ 请编写一个函数,用来删除字符串中的所有空格。

例题:请编写一个函数,用来删除字符串中的所有空格。

例如,输入we l come,则输出为welcome。
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

代码如下:

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
void fun(char*s)
{
    
    
	int i=0;
	char*p=s;
	while(*p)
	{
    
    
		if(*p!=' ')
		{
    
    
			s[i]=*p;
			i++;
		}
	p++;
	}
	s[i]='\0';
}
main()
{
    
    
	char s[81];
	char message[]="Input a string:";
	int n;
	FILE*out;
	printf(message);
	gets(s);
	puts(s);
	fun(s);
	printf("***string:%s\n",s);
	out=fopen("outfile.dat","w");
	fun(message);
	fprintf(out,"%s",message);
	fclose(out);
}

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

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

猜你喜欢

转载自blog.csdn.net/qq_45385706/article/details/111631054
今日推荐