编写一个 C 语言程序,用示例替换字符串中的字符的第一次出现。
C 语言程序:替换字符串中字符的第一次出现 示例 1
此 C 程序允许用户输入一个字符串(或字符数组)和一个字符值。接下来,它将搜索并替换字符串中字符的第一次出现。
/* C Program to Replace First Occurrence of a Character in a String */
#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 Replace : ");
scanf("%c", &ch);
getchar();
printf("\n Please Enter the New Character : ");
scanf("%c", &Newch);
for(i = 0; str[i] != '\0'; i++)
{
if(str[i] == ch)
{
str[i] = Newch;
break;
}
}
printf("\n The Final String after Replacing First occurrence of '%c' with '%c' = %s ", ch, Newch, str);
return 0;
}

首先,我们使用For 循环来迭代字符串中的每个字符。
for(i = 0; i <= strlen(str); i++)
{
if(str[i] == ch)
{
str[i] = Newch;
break;
}
}
str[] = Hello World
ch = l
Newch = @
For 循环第一次迭代:for(i = 0; i <= strlen(str); i++)
在 C 语言的 While 循环中,我们使用If 语句来检查 str[0] 是否等于用户指定的字符。
if(str[i] == ch) => if(H == l)
上述条件为假。因此,i 将递增,Flag 仍为 0。
第二次迭代:for(i = 1; 1 <= 11; 1++)
if(str[i] == ch) => if(e == l) – 条件为假。因此,i 将递增,Flag 仍为 0。
第三次迭代:for(i = 2; 2 <= 11; 2++)
if(str[i] == ch) => if(l == l) – 条件为真
str[i] = Newch
str[2] = @
并且,break 语句将退出循环。
接下来,我们使用 printf 语句打印最终字符串。
printf("\n The Final String after Replacing First occurrence of '%c' with '%c' = %s ", ch, Newch, str);
C 语言程序:替换字符串中字符的第一次出现 示例 2
此程序替换第一个字符出现与上面相同。在这里,我们只是将 For 循环替换为While 循环。
/* C Program to Replace First Occurrence of a Character in a String */
#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 Replace : ");
scanf("%c", &ch);
getchar();
printf("\n Please Enter the New Character : ");
scanf("%c", &Newch);
while(i <= strlen(str))
{
if(str[i] == ch)
{
str[i] = Newch;
break;
}
i++;
}
printf("\n The Final String after Replacing First occurrence of '%c' with '%c' = %s ", ch, Newch, str);
return 0;
}
Please Enter any String : tutorial gateway
Please Enter the Character that you want to Replace : t
Please Enter the New Character : M
The Final String after Replacing First occurrence of 't' with 'M' = Mutorial gateway
程序:替换字符串中字符的第一次出现 示例 3
此程序替换第一个字符出现与第一个示例相同。但这次,我们使用了函数概念来分离逻辑。
/* C Program to Replace First Occurrence of a Character in a String */
#include <stdio.h>
#include <string.h>
void Replace_FirstOccurrence(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_FirstOccurrence(str, ch, Newch);
printf("\n The Final String after Replacing First occurrence of '%c' with '%c' = %s ", ch, Newch, str);
return 0;
}
void Replace_FirstOccurrence(char *str, char ch, char Newch)
{
int i;
for(i = 0; str[i] != '\0'; i++)
{
if(str[i] == ch)
{
str[i] = Newch;
break;
}
}
}
Please Enter any String : learn c programming
Please Enter the Character that you want to Replace : e
Please Enter the New Character : @
The Final String after Replacing First occurrence of 'e' with '@' = l@arn c programming