编写一个 C 语言程序,用示例替换字符串中某个字符的最后一次出现。
C 语言程序:替换字符串中某个字符的最后一次出现 示例 1
此 C 程序允许用户输入一个字符串(或字符数组)和一个字符值。接下来,它将查找并替换字符串中该字符的最后一次出现。
/* C Program to Replace Last Occurrence of a Character in a String */
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch, Newch;
int i, index;
index = -1;
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)
{
index = i;
}
}
if(index != -1)
{
str[index] = Newch;
}
printf("\n The Final String after Replacing Last occurrence of '%c' with '%c' = %s ", ch, Newch, str);
return 0;
}

首先,我们使用 For 循环 迭代 C 语言 C Programming 字符串中的每个字符。
for(i = 0; i <= strlen(str); i++)
{
if(str[i] == ch)
{
index = i;
}
}
接下来,我们使用 If 语句 来检查索引值是否不等于 -1。如果为 True,则执行 str[index] = Newch;
str[] = Hello
ch = l
Newch = @
Index = -1
For 循环第一次迭代:for(i = 0; i <= strlen(str); i++)
条件为 True,因为 i <= 5。
在 While 循环中,我们使用 If 语句 来检查 str[0] 是否等于用户指定的字符。
if(str[i] == ch) => if(H == l) – 条件为 False。因此,i 值将被递增,Index 值仍为 -1。
第二次迭代:for(i = 1; 1 <= 5; 1++)
if(str[i] == ch) => if(e == l) – 条件为 False。因此,i 将被递增,Index 值仍为 -1。
第三次迭代:for(i = 2; 2 <= 5; 2++)
if(str[i] == ch) => if(l == l) – 条件为 True
Index = i
index = 2
第三次迭代:for(i = 3; 3 <= 5; 3++)
if(str[i] == ch) => if(l == l) – 条件为 True
index = i = 3
对剩余的迭代执行相同的操作。
接下来,我们使用 If 语句 来检查索引值是否等于 -1。这里,条件为 True,所以,
str[index] =Newch
str[3] = @
最后,我们使用 printf 语句打印最终的 字符串。
printf("\n The Last Occurrence of the Search Element '%c' is at Position %d ", ch, i + 1);
C 语言程序:替换字符串中某个字符的最后一次出现 示例 2
这个 程序 用于替换最后一个字符的出现,与上面相同。这里,我们只是将 For 循环替换为 While 循环。
/* C Program to Replace Last Occurrence of a Character in a String */
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch, Newch;
int i, index;
index = -1;
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)
{
index = i;
}
i++;
}
if(index != -1)
{
str[index] = Newch;
}
printf("\n The Final String after Replacing Last 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 : #
The Final String after Replacing Last occurrence of 't' with '#' = tutorial ga#eway
C 语言程序:替换字符串中某个字符的最后一次出现 示例 3
这个 程序 用于替换最后一个字符的出现,与第一个示例相同。不过这次,我们应用了 函数 的概念来分离逻辑。
/* C Program to Replace Last Occurrence of a Character in a String */
#include <stdio.h>
#include <string.h>
void Replace_LastOccurrence(char *str, char ch, char Newch);
int main()
{
char str[100], ch, Newch;
int i, index;
index = -1;
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_LastOccurrence(str, ch, Newch);
printf("\n The Final String after Replacing Last occurrence of '%c' with '%c' = %s ", ch, Newch, str);
return 0;
}
void Replace_LastOccurrence(char *str, char ch, char Newch)
{
int i, index;
index = -1;
for(i = 0; str[i] != '\0'; i++)
{
if(str[i] == ch)
{
index = i;
}
}
if(index != -1)
{
str[index] = Newch;
}
}
Please Enter any String : c programming
Please Enter the Character that you want to Replace : m
Please Enter the New Character : $
The Final String after Replacing Last occurrence of 'm' with '$' = c program$ing