C Program to find Last Occurrence of a Character in a String

How to write a C Program to find Last Occurrence of a Character in a String with an example?.

C Program to find Last Occurrence of a Character in a String Example 1

This program allows the user to enter a string (or character array), and a character value. Next, it will search and find the last occurrence of a character inside a string using If Else Statement.

/* 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

First, we used C For Loop to iterate each character in a C String.

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

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

C While Loop First Iteration: while (str[ i ] != 0)
The condition is True because str[0] = H.

Within the C Programming While Loop, we used C If statement to check whether the str[0] is equal to a user-specified character or not

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

The above condition is false. So, i will increment, and the Index value is still -1.

Second Iteration: while (str[ 1 ] != 0)
The condition is True because str[1] = e.

if(str[i] == ch) => if(e == l) – condition is false. So, i value will be incremented, and Index value is still -1.

Third Iteration: while (str[ 2 ] != 0) – condition is True because str[2] = l.

if(str[i] == ch) => if(l == l) – condition is True.
Index  = i
index  = 2

Third Iteration: while (str[ 3 ] != 0) – condition is True because str[3] = l.

if(str[i] == ch) => if(l == l) – condition is True.
index  = i = 3

Do the same for remaining iterations.

Next, we used the C If Else Statement to check the index value is equal to -1. Here, the condition is False. So, the statement inside the else block will execute.

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

Here, i is the index position (start with zero), and i + 1 is the actual position.

C Program to find Last Occurrence of a Character in a String Example 2

This C program for last character occurrence is the same as above. Here, we just replaced the For Loop with a While Loop.

#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 Program to find Last Occurrence of a Character in a String Example 3

This C program for last Character occurrence is the same as the first example, but this time we used the C Functions concept to separate the logic.

#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 Program to find Last Occurrence of a Character in a String Example 4

This C program is same as the first example, but this time we are using the C pointers concept.

/* Fnd 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 

Also Read