问题 问答题

请编写—个函数,用来删除字符串中的所有空格。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
试题程序:
#include <stdio.h>
#include <ctype.h>
#include<conio.h>
void fun (char *str)


main()
char str[81];
char Msg[]="Input a string:";
int n;
printf(Msg);
gets(str);
puts(str);
fun(str);
printf("***str:%s\n", str);

答案

参考答案:void fun(char *str)
{ int i=0;
char *p=str;
while(*p)/*遍历字符串*/
{
/*如果当前元素不为空格*/
if(*平p!=’ ’)
{
/*将当前元素保存到str中*/
str[il=*p;
i++;
}
p++;
}
str[i]=’\0’; /*字符串最后加上结束标记符’\0’*/
}

解析: 题目要求删除空格,也就是重新保存空格以外的其他字符。通过循环删除字符串中的每一个空格,将非空格字符进行重新保存。

解答题
判断题