编写一个 C 语言程序,从字符串中删除所有出现的字符,附带示例。
C 语言删除字符串中所有指定字符的程序 示例 1
此程序允许用户输入一个字符串(或字符数组),以及一个字符值。接下来,此 C 语言程序将查找并删除字符串中某个字符的所有出现。
/* C Program to Remove All Occurrences of a Character in a String */
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch;
int i, len, j;
printf("\n Please Enter any String : ");
gets(str);
printf("\n Please Enter the Character that you want to Remove : ");
scanf("%c", &ch);
len = strlen(str);
for(i = 0; i < len; i++)
{
if(str[i] == ch)
{
for(j = i; j < len; j++)
{
str[j] = str[j + 1];
}
len--;
i--;
}
}
printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch, str);
return 0;
}

首先,我们使用 For 循环 遍历字符串中的每个字符。在循环中,我们使用 If 语句 检查单个字符是否等于用户指定的字符。如果为真,我们则使用另一个 for 循环来移动字符。
for(i = 0; i < len; i++)
{
if(str[i] == ch)
{
for(j = i; j < len; j++)
{
str[j] = str[j + 1];
}
len--;
i--;
}
}
str[] = Hello
ch = l
len = 5
第一个 For 循环 – 第一次迭代:for(i = 0; i < len; i++)
条件为 True,因为 0 < 5。
在 C 语言编程 For 循环中,我们使用 If 语句 检查 str[0] 是否等于用户指定的字符。
if(str[i] == ch) => if(H == l)
以上条件为 false。因此,i 将递增。
第一个 For 循环 – 第二次迭代:for(i = 1; 1 < 5; 1++)
if(str[i] == ch) => if(e == l) – 条件为 false。因此,i 将递增。
第一个 For 循环 – 第三次迭代:for(i = 2; 2 < 5; 2++)
if(str[i] == ch) => if(l == l) – 条件为 True。因此,它将进入第二个 For 循环
第二个 For 循环 – 第一次迭代:for(j = i; j < len; j++)
=> for(j = 2; 2 < 5; 2++) – 条件 2 < 5 为 True。
str[i] = str[i + 1]
str[2] = str[3]
str[2] = l
i 值递增
第二次迭代:for(j = 3; 3 < 5; 3++)
str[3] = str[4]
str[3] = 0
i 递增
第三次迭代:for(j = 4; 4 < 5; 4++)
str[4] = str[5]
str[4] = ‘\0’
i 将递增
第四次迭代:for(j = 5; 5 < 5; 5++)
For 循环内的条件失败。因此,它将退出第二个 For 循环
第一个 For 循环 – 第四次迭代:for(i = 3; 3 < 5; 3++)
if(str[i] == ch) => if(l == l) – 条件为 True。因此,它将进入第二个 For 循环
对剩余的迭代执行相同的操作
最后,我们使用 printf 语句打印最终的字符串
printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch, str);
C 语言删除字符串中所有指定字符的程序 示例 2
这个删除所有字符出现的程序与上面相同。在这里,我们只是将 For 循环替换为 While 循环。
/* C Program to Remove All Occurrences of a Character in a String */
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch;
int i, len, j;
i = 0;
printf("\n Please Enter any String : ");
gets(str);
printf("\n Please Enter the Character that you want to Remove : ");
scanf("%c", &ch);
len = strlen(str);
while(i < len)
{
if(str[i] == ch)
{
j = i;
while(j < len)
{
str[j] = str[j + 1];
j++;
}
len--;
i--;
}
i++;
}
printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch, str);
return 0;
}
Please Enter any String : tutorial gateway
Please Enter the Character that you want to Remove : t
The Final String after Removing All Occurrences of 't' = uorial gaeway
C 语言删除字符串中所有指定字符的程序 示例 3
这个 程序 与第一个示例相同,但这次我们使用了 函数 的概念来分离逻辑。
/* C Program to Remove All Occurrences of a Character in a String */
#include <stdio.h>
#include <string.h>
void Remove_AllOccurrence(char *str, char ch);
int main()
{
char str[100], ch;
printf("\n Please Enter any String : ");
gets(str);
printf("\n Please Enter the Character that you want to Remove : ");
scanf("%c", &ch);
Remove_AllOccurrence(str, ch);
printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch, str);
return 0;
}
void Remove_AllOccurrence(char *str, char ch)
{
int i, j, len;
len = strlen(str);
for(i = 0; i < len; i++)
{
if(str[i] == ch)
{
for(j = i; j < len; j++)
{
str[j] = str[j + 1];
}
len--;
i--;
}
}
}
Please Enter any String : C programming language
Please Enter the Character that you want to Remove : g
The Final String after Removing All Occurrences of 'g' = C prorammin lanuae