版权声明:转载请注明来源~ https://blog.csdn.net/Lth_1571138383/article/details/85321372
Github地址:φ(>ω<*)这里这里。
/*
本次任务为编写一个函数接受一个字符串作为参数,并删除字符串中的空格。
在一个程序中测试该海曙,使用循环读入输入行,知道用户输入一行空行。
该程序应该应用该函数读取每个输入的字符串,并显示处理后的结果。
*/
#include<stdio.h>
#define o 100
void del(char * s1);
int main(void)
{
int i = 0;
char name[o] = {};
while(true)
{
printf("Please input(Empty line to quit): \n");
fgets(name, o, stdin);
if(name[0] == '\n')
{
break;
}
// 处理 fgets()的换行符。
i = 0;
while(name[i] != '\n')
{
i++;
}
if(name[i] == '\n')
{
name[i] = '\0';
}
del(name);
}
printf("Bye~\n");
getchar();
return 0;
}
void del(char * s1)
{
// 思路:这个删除操作啊。。。我打算用循环,遍历name数组的值,然后用另外一个数组储存字符串。
// 把纯字符文本储存完毕再遍历一遍拷贝进去。
// 所以这个指向开头的指针很重要。。。。
int i = 0;
char name2[o] = {};
char * star;
star = s1;
// 这是第一步,把纯文本储存进一个数组。
while(*s1 != '\0')
{
// 这个拷贝就是把没有空格字符的纯字符文本拷贝到一个新的数组。
if(*s1 == ' ')
{
s1++;
continue;
}
name2[i] = *s1;
s1++;
i++;
}
name2[i] = '\0'; // 别忘了末尾的空字符哦。。
// 这是第二步,重新给name数组赋值。
i = 0;
s1 = star;
while(name2[i] != '\0')
{
*s1 = name2[i];
printf("\nHow about this s1 : %c. and i %d..\n", *s1, i);
s1++;
i++;
}
*(s1) = '\0';
/*
这样重写就相当于把纯文本文档覆盖原数组了,然后再添加一个\0。
这样就是两个字符串,一个是纯文本文档+\0;另外一个是原数组剩下的字符加+\0。
这么说应该懂我的思路了吧。。虽然上面有写。
*/
printf("\nThis is the result:\n");
while(*star != '\0')
{
printf("%c", *star);
star++;
}
putchar('\n');
return;
}