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

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

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

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

/* C Program to find Last Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, index;
  	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);
  	
  	for(i = 0; i <= strlen(str); i++)
  	{
  		if(str[i] == ch)  
		{
  			index = i;	
 		}
	}
    if(index == -1)
  	{
  		printf("\n Sorry!! We haven't found the the Search Element '%c' ", ch);
	}
	else
	{
		printf("\n The Last Occurrence of the Search Element '%c'' at Position %d ", ch, index + 1);
	}	
  	return 0;
}
C Program to find Last Occurrence of a Character in a String 1

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

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

str[] = Hello World
ch = l
Index = -1

While Loop 第一次迭代: while (str[ i ] != 0)
条件为 True,因为 str[0] = H。

C Programming While Loop 中,我们使用 If statement 检查 str[0] 是否等于用户指定的字符。

if(str[i] == ch) => if(H == l)

上述条件为假。因此,i 将递增,Index 值仍为 -1。

第二次迭代: while (str[ 1 ] != 0)
条件为真,因为 str[1] = e。

if(str[i] == ch) => if(e == l) – 条件为假。 因此,i 值将递增,Index 值仍为 -1。

第三次迭代: while (str[ 2 ] != 0) – 条件为真,因为 str[2] = l。

if(str[i] == ch) => if(l == l) – 条件为真。
Index = i
index = 2

第三次迭代: while (str[ 3 ] != 0) – 条件为真,因为 str[3] = l。

if(str[i] == ch) => if(l == l) – 条件为真。
index  = i = 3

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

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

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

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

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

C 程序(用于查找字符最后一次出现)与上述程序相同。这里,我们只是将 For Loop 替换为 While Loop

/* C Program to find Last Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, index;
  	i = 0;
  	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);
  	
  	while(str[i] != '\0')
  	{
  		if(str[i] == ch)  
		{
  			index = i;	
 		}
 		i++;
	}
    if(index == -1)
  	{
  		printf("\n Sorry!! We haven't found the the Search Element '%c' ", ch);
	}
	else
	{
		printf("\n The Last Occurrence of the Search Element '%c'' at Position %d ", ch, index + 1);
	}	
  	return 0;
}
 Please Enter any String :  tutorial gateway

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

 The Last Occurrence of the Search Element 'a'' at Position 15

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

C 程序(用于查找字符最后一次出现)与第一个示例相同,但这次我们使用了 Functions 的概念来分离逻辑。

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

int Find_LastCharcater(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_LastCharcater(str, ch); 
  	
    if(index == -1)
  	{
  		printf("\n Sorry!! We haven't found the Search Character '%c' ", ch);
	}
	else
	{
		printf("\n The Last Occurrence of the Search Element '%c' at Position %d ", ch, index + 1);
	}	
  	return 0;
}

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

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

 The Last Occurrence of the Search Element 'n' at Position 18 

C 语言查找字符串中字符最后一次出现位置的程序 示例 4

程序 与第一个示例相同,但这次我们使用了 pointers 的概念。

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

int Find_LastCharcater(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_LastCharcater(str, ch); 
  	
    if(index == -1)
  	{
  		printf("\n Sorry!! We haven't found the Search Character '%c' ", ch);
	}
	else
	{
		printf("\n The Last Occurrence of the Search Element '%c' at Position %d ", ch, index + 1);
	}	
  	return 0;
}

int Find_LastCharcater(char *str, char ch)
{
	int i, index;
	i = 0;
	index = -1;
	
	while(*str)
	{
		if(*str == ch)
		{
			index = i;
		} 
		i++;
		str++; 
	}
	return index;
}
 Please Enter any String :  welcome to tutorial gateway

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

 The Last Occurrence of the Search Element 'e' at Position 24