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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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