Write a function that removes all whitespace from a string
Error analysis:
This is because when the character constant is a space character, you cannot enter only two single quotes, you must also enter a space between the two single quotes; Otherwise, an error will be reported when compiling, prompting null characters, as shown in the figure:
After consulting materials, it is not allowed to put nothing between two single quotation marks. After modification, it is shown in the following figure:
Code:
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
void fun (char *str)
{
int i=0;
char *p=str;
while(*p){
if(*p!=' '){ //Del Space key
str[i++]=*p;
}
p++;
}
str[i]='\0'; //add Terminator
}
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);
/******************************/
}