编程题
请编写一个函数,用来删除字符串中的所有空格。
例如,输入asd af aa z67,则输出为asdafaaz67。
注意:部分源程序给出如下。
请勿改动主函数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;
FILE *out;
printf(Msg) ;
gets(str);
puts(str);
fun(str);
printf("*** str: %s\n", str);
out=fopen ("out.dat", "w");
fun(Msg);
fprintf (out, "%s", Msg);
fclose (out );
}
答案是:
void fun(char *str)
{
int i=0;
char *p=str;
while(*p)
{
if(*p!=’ ’)
{
str[i]=*p;
i++;
}
p++;
}
str[I]=’\0’;
}