C Program to Remove all Characters in a String Except Alphabets

Updated on September 30, 2024
Remove all Characters in a String Except Alphabets header image

Introduction

Removing non-alphabet characters from a string is a common task in programming, often required when cleaning or processing textual data. In C programming, this task involves examining each character in the string and determining whether it falls within the alphabetic range defined by the ASCII standard.

In this article, you will learn how to create a C program that efficiently removes all characters from a string except for alphabets. We will discuss different methods and provide code examples to help you understand how to implement these solutions effectively.

Basic Method Using ASCII Values

Remove Non-Alphabet Characters

  1. Start by defining a function that takes a string as input.

  2. Loop through each character of the string.

  3. Check if the character is an alphabet using ASCII values.

  4. If it is an alphabet, append it to a result string.

  5. Finally, copy the result back to the original string or return it.

    c
    #include <stdio.h>
    
    void filterAlphabets(char *str) {
        char result[strlen(str) + 1];
        int j = 0;
        for (int i = 0; str[i] != '\0'; i++) {
            if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
                result[j++] = str[i];
            }
        }
        result[j] = '\0';  // Null-terminate the result string
        strcpy(str, result);  // Optionally copy back to original string
    }
    
    int main() {
        char str[] = "Hello! 123 World.";
        filterAlphabets(str);
        printf("Filtered String: %s\n", str);
        return 0;
    }
    

    This code defines the filterAlphabets function which iterates over each character in the string str and checks if the character is an alphabet. If true, it stores it in the result string. The resultant string contains only alphabetic characters, which is then printed.

Efficient In-place Modification

Modify String Directly Without Extra Storage

  1. Modify the function to alter the string in place.

  2. Utilize two pointers approach to manage the removal efficiently.

  3. The first pointer iterates over the characters, and the second updates the position for alphabets.

    c
    #include <stdio.h>
    
    void filterAlphabetsInPlace(char *str) {
        int readPos = 0, writePos = 0;
        while (str[readPos] != '\0') {
            if ((str[readPos] >= 'a' && str[readPos] <= 'z') || (str[readPos] >= 'A' && str[readPos] <= 'Z')) {
                str[writePos++] = str[readPos];
            }
            readPos++;
        }
        str[writePos] = '\0';  // Null-terminate the string
    }
    
    int main() {
        char str[] = "Hello! 123 World.";
        filterAlphabetsInPlace(str);
        printf("Filtered String: %s\n", str);
        return 0;
    }
    

    This version of filterAlphabetsInPlace modifies the string directly, eliminating the need for extra storage space. It uses two pointers to control the reading and writing positions, ensuring the removal of non-alphabetic characters is done efficiently.

Conclusion

Remove non-alphabet characters in a string using C requires understanding of character properties and basic string manipulation techniques. The examples provided demonstrate both a basic and an enhanced approach to achieve this, with the latter optimizing space usage by modifying the string in-place. Implement these methods in your text processing tasks to maintain clean and readable strings that consist solely of alphabetic characters. By mastering these techniques, ensure robust and efficient string handling in your C programs.