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

Write a C Program to Replace Last Occurrence of a Character in a String with example.

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

This C program allows the user to enter a string (or character array), and a character value. Next, it will find and replace last occurrence of a character inside a string.

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

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

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

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

Next, we used the If Statement to check the index value is not equal to -1. If it is True then execute str[index] = Newch;
str[] = Hello
ch = l
Newch = @
Index = -1

For Loop First Iteration: for(i = 0; i <= strlen(str); i++)
The condition is True because i <= 5.

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

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

Second Iteration: for(i = 1; 1 <= 5; 1++)
if(str[i] == ch) => if(e == l) – condition is false. So, i will increment, and Index value is still -1.

Third Iteration: for(i = 2; 2 <= 5; 2++)
if(str[i] == ch) => if(l == l) – condition is True
Index  = i
index  = 2

Third Iteration: for(i = 3; 3 <= 5; 3++)
if(str[i] == ch) => if(l == l) – condition is True
index  = i = 3

Do the same for remaining iterations.

Next, we used the If Statement to check the index value is equal to -1. Here, the condition is True so,
str[index]  =Newch
str[3] = @

At last, we used the printf statement to print the final string

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

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

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

/* C Program to Replace Last Occurrence of a Character in a String */

#include <stdio.h>
#include <string.h> 

int main()
{
  	char str[100], ch, Newch;
  	int i, index;
  	index = -1;
  	i = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Replace :  ");
  	scanf("%c", &ch);  	
  	getchar();
  	
  	printf("\n Please Enter the New Character :  ");
  	scanf("%c", &Newch);
  	
  	while(i <= strlen(str))
  	{
  		if(str[i] == ch)  
		{
  			index = i;
 		}
 		i++;
	}
	
	if(index != -1)
  	{
  		str[index] = Newch;
	}

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

 Please Enter the Character that you want to Replace :  t

 Please Enter the New Character :  #

 The Final String after Replacing Last occurrence of 't' with '#' = tutorial ga#eway

C Program to Replace Last Occurrence of a Character in a String Example 3

This program to replace the last character occurrence is the same as the first example. Still, this time, we applied the Functions concept to separate the logic.

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

void Replace_LastOccurrence(char *str, char ch, char Newch);
 
int main()
{
  	char str[100], ch, Newch;
  	int i, index;
  	index = -1;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Replace :  ");
  	scanf("%c", &ch);
  	
  	getchar();
  	
  	printf("\n Please Enter the New Character :  ");
  	scanf("%c", &Newch);
  	
  	Replace_LastOccurrence(str, ch, Newch);

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

void Replace_LastOccurrence(char *str, char ch, char Newch)
{
	int i, index;
  	index = -1;

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

 Please Enter the Character that you want to Replace :  m

 Please Enter the New Character :  $

 The Final String after Replacing Last occurrence of 'm' with '$' = c program$ing

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.