C Program To Remove Last Occurrence of a Character in a String

C Program To Remove Last Occurrence of a Character in a String

C program to remove last occurrence of a character in a string; Through this tutorial, we will learn how to remove last occurrence of a character in a string using for loop, while loop and functions in c programs.

Programs and Algorithm to Remove Last Occurrence of a Character in a String in C

  • Algorithm to remove last occurrence of a character
  • C Program to Remove Last Occurrence of a Character in a String using For Loop
  • C Program to Remove Last Occurrence of a Character in a String using While Loop
  • C Program to Remove Last Occurrence of a Character in a String using Function

Algorithm to remove last occurrence of a character

Use the following algorithm to write a c program to remove last occurrence of a character in given string; as follows:

  1. Start program.
  2. Take input string from user, store it in some variable.
  3. Take Input character to remove from user, store it in some variable.
  4. Find and remove last occurrence of the given character in string.
  5. Print result
  6. End program

C Program to Remove Last Occurrence of a Character in a String using For Loop

/* C Program to Remove Last Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, index, len;
  	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);
  	
	len  = strlen(str);
  	
  	for(i = 0; i < len; i++)
  	{
  		if(str[i] == ch)  
		{
  			index = i;	
 		}
	}
    if(index != -1)
  	{ 		
  		i = index;
  	
	  	while(i < len)
  		{
  			str[i] = str[i + 1];
			i++;  
		}
	}

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

The output of the above c program; as follows:

Please Enter any String :  hello world
Please Enter the Character that you want to Search for :  o
The Final String after Removing Last Occurrence of 'o' = hello wrld 

C Program to Remove Last Occurrence of a Character in a String using While Loop

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

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

The output of the above c program; as follows:

Please Enter any String :  programming world
Please Enter the Character that you want to Search for :  o
The Final String after Removing Last Occurrence of 'o' = programming wrld 

C Program to Remove Last Occurrence of a Character in a String using Function

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

void Remove_LastOccurrence(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 Search for :  ");
  	scanf("%c", &ch);
  	
	Remove_LastOccurrence(str, ch);

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

void Remove_LastOccurrence(char *str, char ch)
{
	int i, index, len;
	
	len = strlen(str);
	
	for(i = 0; i < len; i++)
  	{
  		if(str[i] == ch)  
		{
  			index = i;	
 		}
	}
	
    if(index != -1)
  	{ 		
  		i = index;
  	
	  	while(i < len)
  		{
  			str[i] = str[i + 1];
			i++;  
		}
	}
}

The output of the above c program; as follows:

Please Enter any String :  remove last character
Please Enter the Character that you want to Search for :  r
The Final String after Removing Last Occurrence of 'r' = remove last characte 

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *