C 语言程序:删除字符串中字符的首次出现

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

C 语言程序:删除字符串中字符首次出现的示例 1

此 C 程序允许用户输入一个字符串(或字符数组)和一个字符值。接下来,它将在字符串中搜索并删除该字符的首次出现。

/* C Program to Remove First Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, len;
  	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 && str[i] != ch)
	{
		i++;
	}  
  	
  	while(i < len)
  	{
  		str[i] = str[i + 1];
		i++;  
	}
	printf("\n The Final String after Removing First occurrence of '%c' = %s ", ch, str);
	
  	return 0;
}
C Program to Remove First Occurrence of a Character in a String 1

用户在此 C 程序中输入的数值
str[] = Hello
ch = l

While 循环第一次迭代:while(i < len && str[i] != ch)
=> while(0 < 5 && H != l) – 条件为真
i 将递增

第二次迭代: while(1 < 5 && e != l) – 条件为真
i 将递增

第三次迭代: while(2 < 5 && l != l)
条件为假。因此,它将退出 While 循环。请记住,i 的值为 2
i 将递增

接下来,编译器将进入第二个 while 循环

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

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

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

第四次迭代: while(5 < 5)
条件失败。因此,编译器将退出 While 循环

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

printf("\n The Final String after Removing First occurrence of '%c' = %s ", ch, str);

程序:删除字符串中字符首次出现的示例 2

此程序用于删除字符的首次出现,与上面相同。在这里,我们只是将 While 循环替换为 For 循环

/* C Program to Remove First Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, len;
 
  	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 && str[i] != ch; i++);
  	
  	while(i < len)
  	{
  		str[i] = str[i + 1];
		i++;  
	}
	printf("\n The Final String after Removing First occurrence of '%c' = %s ", ch, str);
	
  	return 0;
}
 Please Enter any String :  tutorial gateway

 Please Enter the Character that you want to Remove :  a

 The Final String after Removing First occurrence of 'a' = tutoril gateway

程序:删除字符串中字符首次出现的示例 3

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

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

void Remove_FirstOccurrence(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_FirstOccurrence(str, ch);
	
	printf("\n The Final String after Removing First occurrence of '%c' = %s ", ch, str);
	
  	return 0;
}

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

 Please Enter the Character that you want to Remove :  g

 The Final String after Removing First occurrence of 'g' = c proramming