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

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

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

Programs and Algorithm To Replace All Occurrence of a Character in a String in C

  • Algorithm To Replace All Occurrence of a Character in a String
  • C Program To Replace All Occurrence of a Character in a String using For Loop
  • C Program To Replace All Occurrence of a Character in a String using While Loop
  • C Program To Replace All Occurrence of a Character in a String using Function

Algorithm To Replace All Occurrence of a Character in a String

Use the following algorithm to write a program to replace all occurrence of a character in a string; as follows:

  1. Input string from user, store it in some variable.
  2. Input character to replace and character new character from user, store it in some variables.
  3. Iterate a loop from start of string str to end.
  4. Inside the loop, swap all old character with new character and terminate the loop.
  5. Print result.

C Program To Replace All Occurrence of a Character in a String using For Loop

/* C Program to Replace All Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch, Newch;
  	int i;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("Enter a character replace: ");
  	scanf("%c", &ch);
  	
  	getchar();
  	
    printf("\nEnter character to replace  with  %c : ",ch);
  	scanf("%c", &Newch);
  	
  	for(i = 0; i <= strlen(str); i++)
  	{
  		if(str[i] == ch)  
		{
  			str[i] = Newch;
 		}
	}
	
	printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s ", ch, Newch, str);
	
  	return 0;
}

The output of the above c program; as follows:

Please Enter any String :  c [email protected]
Enter a character replace: @
Enter character to replace  with  @ : a
The Final String after Replacing All Occurrences of '@' with 'a' = c programmer 

C Program To Replace All Occurrence of a Character in a String using While Loop

/* C Program to Replace All Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch, Newch;
  	int i = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
    printf("Enter a character replace: ");
  	scanf("%c", &ch);
  	
  	getchar();
  	
  	printf("\n Please Enter the New Character :  ");
  	scanf("%c", &Newch);
  	
  	while(str[i] != '
/* C Program to Replace All Occurrence of a Character in a String */
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch, Newch;
int i = 0;
printf("\n Please Enter any String :  ");
gets(str);
printf("Enter a character replace: ");
scanf("%c", &ch);
getchar();
printf("\n Please Enter the New Character :  ");
scanf("%c", &Newch);
while(str[i] != '\0')
{
if(str[i] == ch)  
{
str[i] = Newch;
}
i++;
}
printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s ", ch, Newch, str);
return 0;
}
') { if(str[i] == ch) { str[i] = Newch; } i++; } printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s ", ch, Newch, str); return 0; }

The output of the above c program; as follows:

Please Enter any String :  [email protected]@@o
Enter a character replace: @
Please Enter the New Character :  l
The Final String after Replacing All Occurrences of '@' with 'l' = helllo 

C Program To Replace All Occurrence of a Character in a String using Function

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

void Replace_AllOccurrence(char *str, char ch, char Newch);
 
int main()
{
  	char str[100], ch, Newch;
  	int i = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("Enter a character replace: ");
  	scanf("%c", &ch);
  	
  	getchar();
  	
  	printf("\nEnter character to replace  with  %c : ",ch);
  	scanf("%c", &Newch);
  	
  	Replace_AllOccurrence(str, ch, Newch);
  	
  	printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s ", ch, Newch, str);
	
  	return 0;
}

void Replace_AllOccurrence(char *str, char ch, char Newch)
{
	int i;

	for(i = 0; str[i] != '
/* C Program to Replace All Occurrences of a Character in a String */
#include <stdio.h>
#include <string.h>
void Replace_AllOccurrence(char *str, char ch, char Newch);
int main()
{
char str[100], ch, Newch;
int i = 0;
printf("\n Please Enter any String :  ");
gets(str);
printf("Enter a character replace: ");
scanf("%c", &ch);
getchar();
printf("\nEnter character to replace  with  %c : ",ch);
scanf("%c", &Newch);
Replace_AllOccurrence(str, ch, Newch);
printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s ", ch, Newch, str);
return 0;
}
void Replace_AllOccurrence(char *str, char ch, char Newch)
{
int i;
for(i = 0; str[i] != '\0'; i++)
{
if(str[i] == ch)
{
str[i] = Newch;
}  
}
}
'; i++) { if(str[i] == ch) { str[i] = Newch; } } }

The output of the above c program; as follows:

Please Enter any String :  c programming
Enter a character replace: m
Enter character to replace  with  m : @
The Final String after Replacing All Occurrences of 'm' with '@' = c [email protected]@ing 

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 *