C 语言查找字符串中字符首次出现位置的程序

如何编写一个 C 语言程序来查找字符串中字符的首次出现,并附带示例?

C 语言查找字符串中字符首次出现位置的程序 示例 1

此程序允许用户输入一个字符串(或字符数组)和一个字符值。接下来,它将使用 If Else 语句搜索并查找字符在字符串中的首次出现。

/* C Program to find First Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, Flag;
  	Flag = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Search for :  ");
  	scanf("%c", &ch);
  	
  	for(i = 0; i <= strlen(str); i++)
  	{
  		if(str[i] == ch)  
		{
  			Flag++;
			break;    	
 		}
	}
    if(Flag == 0)
  	{
  		printf("\n Sorry!! We haven't found the Search Character '%c' ", ch);
	}
	else
	{
		printf("\n The First Occurrence of the Search Element '%c' is at Position %d ", ch, i + 1);
	}	
  	return 0;
}
C Program to find First Occurrence of a Character in a String 1

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

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

str[] = Hello World
ch = l
Flag = 0

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

在 For 循环内,我们使用 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)
上述条件为真。
Flag++ 表示 Flag  = 1,并且 break 语句 将退出循环。

接下来,我们使用 If Else 语句 来检查 Flag 值是否等于 0。这里,条件为假。因此,else 块中的语句将被执行。

printf("\n The First Occurrence of the Search Element '%c' is at Position %d ", ch, i + 1);

这里,i 是索引位置(从零开始),i + 1 是实际位置。

C 语言查找字符串中字符首次出现位置的程序 示例 2

此程序与上述程序相同。这里,我们只是将 C 语言 For 循环 替换为 While 循环

/* C Program to find First Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, Flag;
  	i = Flag = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Search for :  ");
  	scanf("%c", &ch);
  	
  	while(str[i] != '\0')
  	{
  		if(str[i] == ch)  
		{
  			Flag++;
			break;    	
 		}
 		i++;
	}
    if(Flag == 0)
  	{
  		printf("\n Sorry!! We haven't found the Search Character '%c' ", ch);
	}
	else
	{
		printf("\n The First Occurence of the Search Element '%c' is at Position %d ", ch, i + 1);
	}	
  	return 0;
}
 Please Enter any String :  tutorial gateway

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

 The First Occurence of the Search Element 'a' is at Position 7

C 语言查找字符串中字符首次出现位置的程序 示例 3

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

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

int Find_FirstCharcater(char str[], char ch);

int main()
{
  	char str[100], ch;
  	int index;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Search for :  ");
  	scanf("%c", &ch);
  	
  	index = Find_FirstCharcater(str, ch); 
  	
    if(index == -1)
  	{
  		printf("\n Sorry!! We haven't found the Search Character '%c' ", ch);
	}
	else
	{
		printf("\n The First Occurrence of the Search Element '%c' is at Position %d ", ch, index + 1);
	}	
  	return 0;
}

int Find_FirstCharcater(char str[], char ch)
{
	int i;
	
	for(i = 0; str[i] != '\0'; i++)
	{
		if(str[i] == ch)
		{
			return i;
		}  
	}
	return -1;
}
 Please Enter any String :  learn c programming

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

 The First Occurrence of the Search Element 'm' is at Position 15

查找字符串中字符首次出现位置的程序 示例 4

这个 查找字符首次出现 的程序与第一个示例相同。但是,这次我们使用了 指针 的概念。

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

int Find_FirstCharcater(char *str, char ch);

int main()
{
  	char str[100], ch;
  	int index;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Search for :  ");
  	scanf("%c", &ch);
  	
  	index = Find_FirstCharcater(str, ch); 
  	
    if(index == -1)
  	{
  		printf("\n Sorry!! We haven't found the Search Character '%c' ", ch);
	}
	else
	{
		printf("\n The First Occurrence of the Search Element '%c' is at Position %d ", ch, index + 1);
	}	
  	return 0;
}

int Find_FirstCharcater(char *str, char ch)
{
	int i;
	
	while(*str)
	{
		if(*str == ch)
		{
			return i;
		}
		i++;
		str++;  
	}
	return -1;
}
 Please Enter any String :  programming examples

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

 The First Occurrence of the Search Element 'a' is at Position 6