编写函数void InsertSpace(char *s)要求在字符串中每个非空格后插入一个空格(最后一个字符后不需要插入空格),字符串中本身有的空格保持不变,要求用字符指针处理。
void InsertSpace(char *s)
{
int len = strlen(s);
char *buf = (char *)malloc(2 * len);
char *p = s, *q = buf;
while (*p)
{
*q = *p;
q++;
if (*p != ' ' && *(p + 1) != '\0')
{
*q = ' ';
q++;
}
p++;
}
*q = '\0';
strcpy(s, buf);
free(buf);
}