将字符串“We Are Family!”,去除空格后打印出来。(WeAreFamily!)
#include <stdio.h>
void removespace(char *pStr)
{
char *p1 = pStr;
char *p2 = pStr;
while (*p1 != ' ')//寻找第一个空格
{
p1++;
p2++;
}
while (*p1 != '\0')//循环截止条件:字符串结尾\0
{
if (*++p1 == ' ')//遇到空格跳过
continue;
*p2++ = *p1;//遇到非空格则顺序补上
}
}
int main()
{
char str[] = "We Are Family!";
printf("%s\n", str);
removespace(str);
printf("%s\n", str);
return 0;
}