【发布时间】:2022-01-06 19:02:39
【问题描述】:
我现在正在学习 C 我需要制作一个程序来删除我将从字符串中输入的字符。我看过一个算法,我写了这段代码
#define MAX_LEN 200
int main()
{
char str[MAX_LEN];
char rem;
int i = 0;
printf("Enter the setence:");
gets(str);
printf("\nEnter the char to remove");
rem = getchar();
char* pDest = str;
char* pS= str;
printf("sent:\n%s", str);
while (str[i]!='\0'){
if (*pDest != rem) {
*pDest = *pS;
pDest++;
pS++;
}
else if (*pDest == rem) {
pS++;
}
i++;
}
*pDest = '\0';
while (str[i] != '\0') {
printf("number%d", i);
putchar(str[i]);
printf("\n");
i++;
}
}
但它什么也不返回,就像 str 得到的值一样,我认为 \0 并且什么也不返回。 你能帮我找出问题吗?
【问题讨论】:
-
while (str[i]!='\0'){ ...i++;}后跟while (str[i]!='\0'){ ... }中间没有重置i。所以没有进入第二个循环。