C 语言在字符串中替换所有出现的字符

编写一个 C 语言程序,用示例替换字符串中的所有字符。

C 语言程序在字符串中替换所有出现的字符示例

此程序允许用户输入一个字符串(或字符数组)和一个字符值。接下来,它将搜索并替换字符串中出现的所有字符。

#include <stdio.h>
#include <string.h>

int main()
{
char str[100], ch, Newch;
int i;

printf("\n Please Enter any String : ");
gets(str);

printf("\n Please Enter the Character that you want to Search for : ");
scanf("%c", &ch);

getchar();

printf("\n Please Enter the New Character : ");
scanf("%c", &Newch);

for(i = 0; i <= strlen(str); i++)
{
if(str[i] == ch)
{
str[i] = Newch;
}
}

printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s ", ch, Newch, str);

return 0;
}
C Program to Replace All Occurrence of a Character in a String 1

首先,我们使用 For 循环 遍历字符串中的每个字符。

for(i = 0; i < strlen(str); i++)
{
	if(str[i] == ch)  
	{
		str[i] = Newch;  	
	}
}

str[] = Hello
ch = l
Newch = @

For 循环第一次迭代:for(i = 0; i < strlen(str); i++)

C 编程 While 循环中,我们使用 If 语句 来检查 str[0] 是否等于用户指定的字符。

if(str[i] == ch) => if(H == l) – 条件为 False。因此,i 值将被递增。

第二次迭代:for(i = 1; 1 < 5; 1++)

if(str[i] == ch) => if(e == l) – 条件为 False。因此,i 将递增。

第三次迭代:for(i = 2; 2 < 5; 2++)
if(str[i] == ch) => if(l == l) – 条件为 True。
str[i] = Newch
str[2] = @

第四次迭代:for(i = 3; 3 < 5; 3++)
if(str[i] == ch) => if(l == l) – 条件为 True。
str[i] = Newch
str[3] = @

第五次迭代:for(i = 4; 4 < 5; 4++)
if(str[i] == ch) => if(0 == l) – 条件为 False。
str[i] = Newch
str[3] = @
以上条件为 False。

接下来,我们使用 printf 语句打印最终字符串。

printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s ", ch, Newch, str);

C 语言程序在字符串中替换所有出现的字符示例 2

程序 用于替换所有字符出现次数,与上面相同。这里,我们只是将 For 循环替换为 While 循环

#include <stdio.h>
#include <string.h>

int main()
{
char str[100], ch, Newch;
int i = 0;

printf("\n Please Enter any String : ");
gets(str);

printf("\n Please Enter the Character that you want to Search for : ");
scanf("%c", &ch);

getchar();

printf("\n Please Enter the New Character : ");
scanf("%c", &Newch);

while(str[i] != '\0')
{
if(str[i] == ch)
{
str[i] = Newch;
}
i++;
}

printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s ", ch, Newch, str);

return 0;
}
 Please Enter any String :  tutorial gateway

 Please Enter the Character that you want to Search for :  t

 Please Enter the New Character :  $

 The Final String after Replacing All Occurrences of 't' with '$' = $u$orial ga$eway

C 语言程序在字符串中替换所有出现的字符示例 3

程序 用于替换所有字符出现次数,与第一个示例相同。但是,这次我们使用了 函数 概念来分离逻辑。

#include <stdio.h>
#include <string.h>

void Replace_AllOccurrence(char *str, char ch, char Newch);

int main()
{
char str[100], ch, Newch;
int i = 0;

printf("\n Please Enter any String : ");
gets(str);

printf("\n Please Enter the Character that you want to Replace : ");
scanf("%c", &ch);

getchar();

printf("\n Please Enter the New Character : ");
scanf("%c", &Newch);

Replace_AllOccurrence(str, ch, Newch);

printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s ", ch, Newch, str);

return 0;
}

void Replace_AllOccurrence(char *str, char ch, char Newch)
{
int i;

for(i = 0; str[i] != '\0'; i++)
{
if(str[i] == ch)
{
str[i] = Newch;
}
}
}
 Please Enter any String :  Welcome to c programming examples

 Please Enter the Character that you want to Replace :  m

 Please Enter the New Character :  #

 The Final String after Replacing All Occurrences of 'm' with '#' = Welco#e to c progra##ing exa#ples