题目:请编写一个函数,用来删除字符串中的所有空格。
例如,输入I'm going to take the college entrance examination,则输出为 I'mgoingtotakethecollegeentranceexamination。
解析:在我们需要填写的部分,定义了一个指针,用来存储删除字符串空格后所得到的字符串。遍历我们输入的字符串,如果字符不是空格,就把该字符赋到*s中,然后s和str都自增向后移一位,接着判断下一个字符,直到循环结束。最后将指向的位置设为字符串结束符。
/*------------------------------------------------------------------------------
(2)【程序设计】请编写一个函数,用来删除字符串中的所有空格。
例如,输入I'm going to take the college entrance examination,则输出为 I'mgoingtotakethecollegeentranceexamination。
------------------------------------------------------------------------------*/
#include <stdio.h>
void fun (char *str)
{
/**********Program**********/
char *s=str;
while(*str)
{
if(*str!=' ')
{
*s=*str;
s++;
}
str++;
}
*s='\0';
/********** End **********/
}
main()
{
char str[80];
printf("请输入带有空格的字符串:\n");
gets(str);
fun(str);
printf("删除空格后的字符串是: %s\n",str);
}