C语言编程>第二十三周 ⑥ 下列给定程序中函数fun的功能是:删除字符串s中的所有空白字符(包括Tab字符、回车符及换行符)。输入字符串时用 “#”结束输入。

例题:下列给定程序中函数fun的功能是:删除字符串s中的所有空白字符(包括Tab字符、回车符及换行符)。输入字符串时用 “#”结束输入。

例如输入的字符串为"sd ads",则输出"sdads"。
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构。

代码如下:

#include<string.h>
#include<stdio.h>
#include<ctype.h>
void fun(char*s)
{
    
    
	int i,j;
	char ch[80];
	for(i=0,j=0;s[i];i++)
		if(!isspace(*(s+i)))
			ch[j++]=s[i];
	ch[j]='\0';
	strcpy(s,ch);
}
main()
{
    
    
	char ch,str[80];
	int i=0;
	printf("Input a string with # as end:");
	ch=getchar();
	while(ch!='#')
	{
    
    
		str[i]=ch;
		i++;
		ch=getchar();
	}
	str[i]='\0';
	fun(str);
	puts(str);
}

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

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

猜你喜欢

转载自blog.csdn.net/qq_45385706/article/details/113092885