Write a C Program to Replace All Occurrence of a Character in a String with example.
C Program to Replace All 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 replace all occurrences of a character inside a string.
/* 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("\n Please Enter the Character that you want to Search for : "); scanf("%c", &ch); getchar(); printf("\n Please Enter the New Character : "); 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; }

First we used For Loop to iterate each and every character in a String.
for(i = 0; i < strlen(str); i++) { if(str[i] == ch) { str[i] = Newch; } }
str[] = Hello
ch = l
Newch = @
For Loop First Iteration: for(i = 0; i < strlen(str); i++)
Within the C Programming While Loop, we used If Statement to check whether the str[0] is equal to the user specified character or not
if(str[i] == ch) => if(H == l) – condition is false. So, i value will be incremented.
Second Iteration: for(i = 1; 1 < 5; 1++)
if(str[i] == ch) => if(e == l) – condition is false. So, i will increment
Third Iteration: for(i = 2; 2 < 5; 2++)
if(str[i] == ch) => if(l == l) – condition is True
str[i] = Newch
str[2] = @
Fourth Iteration: for(i = 3; 3 < 5; 3++)
if(str[i] == ch) => if(l == l) – condition is True
str[i] = Newch
str[3] = @
Fifth Iteration: for(i = 4; 4 < 5; 4++)
if(str[i] == ch) => if(0 == l) – condition is False.
str[i] = Newch
str[3] = @
The above condition is false.
Next, we used the printf statement to print the final string.
printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s ", ch, Newch, str);
C Program to Replace All Occurrence of a Character in a String Example 2
This program to replace all character occurrences is the same as above. Here, we just replaced the For Lop with 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("\n Please Enter the Character that you want to Search for : "); 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; }
Please Enter any String : tutorial gateway
Please Enter the Character that you want to Search for : t
Please Enter the New Character : $
The Final String after Replacing All Occurrences of 't' with '$' = $u$orial ga$eway
C Program to Replace All Occurrence of a Character in a String Example 3
This program to replace all character occurrences is the same as the first example. However, this time, we used the Functions concept to separate the logic.
/* 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("\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_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; } } }
Please Enter any String : Welcome to c programming examples
Please Enter the Character that you want to Replace : m
Please Enter the New Character : #
The Final String after Replacing All Occurrences of 'm' with '#' = Welco#e to c progra##ing exa#ples