Remove a Specific Character from a String in C
To remove a specific character from a string in C, we can create a new string that skips the unwanted character while copying the rest. This can be achieved using loops, string functions like strcpy() and strchr(), or pointers.
In this tutorial, we will explore multiple ways to accomplish this with detailed explanations.
Examples to Remove a Specific Character
1. Removing a Character Using a Loop
In this example, we iterate through the string and copy characters to a new position, skipping the specified character.
main.c
#include <stdio.h>
#include <string.h>
void removeChar(char *str, char ch) {
int i, j = 0;
int length = strlen(str);
for (i = 0; i < length; i++) {
if (str[i] != ch) {
str[j++] = str[i];
}
}
str[j] = '\0'; // Null-terminate the modified string
}
int main() {
char str[] = "hello world";
char ch = 'o';
removeChar(str, ch);
printf("Modified String: %s\n", str);
return 0;
}
Explanation:
- The function
removeChar()takes a stringstrand the characterchto be removed. - We use a loop to iterate over the string. If the character is not equal to
ch, it is copied to the new position. - The variable
jkeeps track of the index in the modified string. - After processing, we add a null terminator
'\0'at the end to ensure the string remains valid.
Output:
Modified String: hell wrld
2. Removing a Character Using strcpy()
In this approach, we create a temporary string and use strcpy() to copy characters while skipping the specified character.
main.c
#include <stdio.h>
#include <string.h>
void removeCharStrcpy(char *str, char ch) {
char temp[strlen(str) + 1];
int j = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] != ch) {
temp[j++] = str[i];
}
}
temp[j] = '\0';
strcpy(str, temp); // Copy modified string back
}
int main() {
char str[] = "apple papaya";
char ch = 'p';
removeCharStrcpy(str, ch);
printf("Modified String: %s\n", str);
return 0;
}
Explanation:
- A temporary array
tempis created to store the modified string. - A loop iterates through the original string and copies characters to
tempexcept forch. - After copying, we null-terminate
tempto ensure a valid string. - Finally, we use
strcpy()to copy the modified string back to the original string.
Output:
Modified String: ale aaya
3. Removing a Character Using Pointers
Instead of using indexing, we can use pointer manipulation to achieve the same result.
main.c
#include <stdio.h>
void removeCharPointer(char *str, char ch) {
char *src = str, *dst = str;
while (*src) {
if (*src != ch) {
*dst++ = *src;
}
src++;
}
*dst = '\0';
}
int main() {
char str[] = "apple banana cherry";
char ch = 'a';
removeCharPointer(str, ch);
printf("Modified String: %s\n", str);
return 0;
}
Explanation:
- Two pointers
srcanddstare used:srcreads the original string, anddstwrites to modify it. - Characters that are not
chare copied fromsrctodst. - The loop continues until the null terminator is reached.
- The final null terminator ensures a properly terminated string.
Output:
Modified String: pple bnn cherry
Conclusion
In this tutorial, we explored multiple ways to remove a specific character from a string in C:
- Using a loop: Directly modifying the string by shifting elements.
- Using
strcpy(): Copying characters to a temporary string. - Using pointers: Optimized character copying without indexing.
Each method has its advantages, depending on the scenario and memory efficiency. Choose the best approach based on your requirements.
