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

</>
Copy
#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:

  1. The function removeChar() takes a string str and the character ch to be removed.
  2. We use a loop to iterate over the string. If the character is not equal to ch, it is copied to the new position.
  3. The variable j keeps track of the index in the modified string.
  4. 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

</>
Copy
#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:

  1. A temporary array temp is created to store the modified string.
  2. A loop iterates through the original string and copies characters to temp except for ch.
  3. After copying, we null-terminate temp to ensure a valid string.
  4. 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

</>
Copy
#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:

  1. Two pointers src and dst are used: src reads the original string, and dst writes to modify it.
  2. Characters that are not ch are copied from src to dst.
  3. The loop continues until the null terminator is reached.
  4. 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:

  1. Using a loop: Directly modifying the string by shifting elements.
  2. Using strcpy(): Copying characters to a temporary string.
  3. 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.