Write a C Program to Remove All Occurrences of a Character in a String with example.
C Program to Remove All Occurrences 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, this C Program will find and remove all occurrences of a character inside a string.
/* C Program to Remove All Occurrences of a Character in a String */ #include <stdio.h> #include <string.h> int main() { char str[100], ch; int i, len, j; printf("\n Please Enter any String : "); gets(str); printf("\n Please Enter the Character that you want to Remove : "); scanf("%c", &ch); len = strlen(str); for(i = 0; i < len; i++) { if(str[i] == ch) { for(j = i; j < len; j++) { str[j] = str[j + 1]; } len--; i--; } } printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch, str); return 0; }
First, we used For Loop to iterate every character in a String. Within the loop, we used the If Statement to check whether the individual character is equal to a user-specified character or not. If True, then we are using another for loop to shift characters.
for(i = 0; i < len; i++) { if(str[i] == ch) { for(j = i; j < len; j++) { str[j] = str[j + 1]; } len--; i--; } }
str[] = Hello
ch = l
len = 5
First For Loop – First Iteration: for(i = 0; i < len; i++)
The condition is True because 0 < 5.
Within the C Programming For 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)
The above condition is false. So, i will increment.
First For Loop – Second Iteration: for(i = 1; 1 < 5; 1++)
if(str[i] == ch) => if(e == l) – condition is false. So, i will increment.
First For Loop – Third Iteration: for(i = 2; 2 < 5; 2++)
if(str[i] == ch) => if(l == l) – condition is True. So, it will enter into the Second For Loop
Second For Loop – First Iteration: for(j = i; j < len; j++)
=> for(j = 2; 2 < 5; 2++) – The condition 2 < 5 is True.
str[i] = str[i + 1]
str[2] = str[3]
str[2] = l
i value incremented
Second Iteration: for(j = 3; 3 < 5; 3++)
str[3] = str[4]
str[3] = 0
i incremented
Third Iteration: for(j = 4; 4 < 5; 4++)
str[4] = str[5]
str[4] = ‘\0’
i will increment
Fourth Iteration: for(j = 5; 5 < 5; 5++)
Condition inside the For Loop Fails. So, it will exist from the second For Loop
First For Loop – Fourth Iteration: for(i = 3; 3 < 5; 3++)
if(str[i] == ch) => if(l == l) – condition is True. So, it will enter into the Second For Loop
Do the same for remaining iterations
At last, we used the printf statement to print the final string
printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch, str);
C Program to Remove All Occurrences of a Character in a String Example 2
This program to remove all occurrences of a character is the same as above. Here, we just replaced the For Loop with While Loop.
/* C Program to Remove All Occurrences of a Character in a String */ #include <stdio.h> #include <string.h> int main() { char str[100], ch; int i, len, j; i = 0; printf("\n Please Enter any String : "); gets(str); printf("\n Please Enter the Character that you want to Remove : "); scanf("%c", &ch); len = strlen(str); while(i < len) { if(str[i] == ch) { j = i; while(j < len) { str[j] = str[j + 1]; j++; } len--; i--; } i++; } printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch, str); return 0; }
Please Enter any String : tutorial gateway
Please Enter the Character that you want to Remove : t
The Final String after Removing All Occurrences of 't' = uorial gaeway
C Program to Remove All Occurrences of a Character in a String Example 3
This program is the same as the first example, but this time we used the Functions concept to separate the logic.
/* C Program to Remove All Occurrences of a Character in a String */ #include <stdio.h> #include <string.h> void Remove_AllOccurrence(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 Remove : "); scanf("%c", &ch); Remove_AllOccurrence(str, ch); printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch, str); return 0; } void Remove_AllOccurrence(char *str, char ch) { int i, j, len; len = strlen(str); for(i = 0; i < len; i++) { if(str[i] == ch) { for(j = i; j < len; j++) { str[j] = str[j + 1]; } len--; i--; } } }
Please Enter any String : C programming language
Please Enter the Character that you want to Remove : g
The Final String after Removing All Occurrences of 'g' = C prorammin lanuae