C 语言删除字符串中字符最后一次出现

编写一个 C 语言程序,删除字符串中字符的最后一次出现,并附带示例。

C 语言程序删除字符串中字符的最后一次出现示例 1

此程序允许用户输入一个字符串(或字符数组)和一个字符值。接下来,此 C 程序将查找并删除字符串中字符的最后一次出现。

/* C Program to Remove Last Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, index, len;
  	index = -1;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Search for :  ");
  	scanf("%c", &ch);
  	
	len  = strlen(str);
  	
  	for(i = 0; i < len; i++)
  	{
  		if(str[i] == ch)  
		{
  			index = i;	
 		}
	}
    if(index != -1)
  	{ 		
  		i = index;
  	
	  	while(i < len)
  		{
  			str[i] = str[i + 1];
			i++;  
		}
	}

	printf("\n The Final String after Removing Last Occurrence of '%c' = %s ", ch, str);	
  	return 0;
}
C Program to Remove Last Occurrence of a Character in a String 1

首先,我们使用 For 循环 迭代字符串中的每个字符。在循环中,我们检查单个字符是否等于用户指定的字符。如果为真,则将 i 值赋给索引变量。

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

接下来,我们使用 If 语句 检查索引值是否不等于 -1。如果为真,则 C 语言 编译器将移动字符
str[] = tutorial
ch = t
Index = -1

For 循环第一次迭代:for(i = 0; i < len; i++)
条件为真,因为 0 < 8。

在 While 循环中,我们使用 If 语句 检查 str[0] 是否等于给定的字符。

if(str[i] == ch) => if(t == t) – 条件为真。因此,
index = i = 0。

第二次迭代:for(i = 1; 1 < 8; 1++)
if(str[1] == ch) => if(u == l) – 条件为假。因此,i 将递增。

第三次迭代:for(i = 2; 2 <= 5; 2++)
if(str[i] == ch) => if(t == t) – 条件为真。
Index = i
index = 2

对剩余的迭代执行相同的操作。

接下来,我们使用 If 语句 检查索引值是否不等于 -1。这里,条件为真,所以 i = index
i = 2

While 循环第一次迭代:while(i < len)
=> while(2 < 8) – 条件为真
str[i] = str[i + 1]
str[2] = str[3]
str[2] = o
i 将递增

第二次迭代:while(3 < 8)
str[3] = str[4]
str[3] = r
i 将递增

第三次迭代:while(4 < 8)
str[4] = str[5]
str[4] = i
i 将递增

对剩余的迭代执行相同的操作,直到 i 变为 8。

最后,我们使用 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 Remove Last Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, index, len;
  	index = -1;
  	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);
  	
	len  = strlen(str);
  	
  	while(i < len)
  	{
  		if(str[i] == ch)  
		{
  			index = i;	
 		}
 		i++;
	}
    if(index != -1)
  	{ 		
  		i = index;
  	
	  	while(i < len)
  		{
  			str[i] = str[i + 1];
			i++;  
		}
	}

	printf("\n The Final String after Removing Last Occurrence of '%c' = %s ", ch, str);	
  	return 0;
}
 Please Enter any String : tutorial gateway

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

 The Final String after Removing Last Occurrence of 't' = tutorial gaeway

C 语言程序删除字符串中字符的最后一次出现示例 3

这个 程序 删除字符的最后一次出现与第一个示例相同。但是,这次我们使用了 函数 的概念来分离逻辑。

/* C Program to Remove Last Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>

void Remove_LastOccurrence(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 Search for :  ");
  	scanf("%c", &ch);
  	
	Remove_LastOccurrence(str, ch);

	printf("\n The Final String after Removing Last Occurrence of '%c' = %s ", ch, str);	
  	return 0;
}

void Remove_LastOccurrence(char *str, char ch)
{
	int i, index, len;
	
	len = strlen(str);
	
	for(i = 0; i < len; i++)
  	{
  		if(str[i] == ch)  
		{
  			index = i;	
 		}
	}
	
    if(index != -1)
  	{ 		
  		i = index;
  	
	  	while(i < len)
  		{
  			str[i] = str[i + 1];
			i++;  
		}
	}
}
 Please Enter any String :  c programming

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

 The Final String after Removing Last Occurrence of 'g' = c programmin