✓ Link copied to clipboard
C 📅 July 12, 2026 ⏱ 5 min read

C Program to Remove all Characters Except Alphabets

C Program to Remove all Characters Except Alphabets

Sometimes you gotta clean up a string, keeping only the letters. Picture this: you have a string like EduSeekho 123!, and you want just the letters, which would be EduSeekho. Let’s dive into how to do that using C.

Prerequisites for the C Program to Remove Characters

Before writing the program, ensure you're familiar with these concepts:

  • Using the isalpha() function from <ctype.h> to check alphabetic characters.
  • Basic understanding of arrays and strings in C.
  • Utilizing loops for iteration over strings.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler, linking the compiler URL.

Source Code of C Program to Remove all Characters in a String Except Alphabets

Here's the complete source code to remove non-alphabet characters from a string:

remove_characters.cC▶ Run
#include <stdio.h>
#include <ctype.h>

void removeNonAlpha(char str[]) {
    int i = 0, j = 0;
    while (str[i] != '\0') {
        if (isalpha(str[i])) {
            str[j++] = str[i];
        }
        i++;
    }
    str[j] = '\0';
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, 100, stdin);
    removeNonAlpha(str);
    printf("Filtered string: %s", str);
    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Remove all Characters Except Alphabets Program

When you run the program and enter a string, here's what happens:

OUTPUTtext
Enter a string: EduSeekho 123!
Filtered string: EduSeekho
Output of C Program to Remove all Characters in a String Except Alphabets
Output of C Program to Remove all Characters in a String Except Alphabets

Algorithm to Remove Non-Alphabet Characters in C

The algorithm processes each character in the string, checks if it's an alphabet, and constructs a new string with only the alphabetic characters.

  1. Read the input string from the user.
  2. Initialize two indices i and j to 0.
  3. Loop through the string until the null character is reached.
  4. Check if the current character is an alphabet using isalpha().
  5. If yes, copy it to the new position in the string.
  6. Move to the next character in the input string.
  7. Continue this until the end of the string.
  8. Place a null character to terminate the new string.
flowchart TD
  A([Start]) --> B[/Read input string/]
  B --> C[i = 0, j = 0]
  C --> D{string[i] != '\0'?}
  D -->|Yes| E{Is alphabet?}
  E -->|Yes| F[output[j] = string[i]]
  E -->|No| G[i++]
  F --> H[j++]
  H --> G
  G --> D
  D -->|No| I[output[j] = '\0']
  I --> J([End])
Flowchart: the algorithm step by step

How the C Program to Remove Characters Works

Let's go through how each part of the code functions:

  • #include <stdio.h> and #include <ctype.h>: These are needed for input/output functions and character checking.
  • void removeNonAlpha(char str[]): This function takes a string and removes all non-alphabet characters.
  • int i = 0, j = 0;: Initializes counters for reading and writing characters.
  • while (str[i] != '\0'): Loops until the end of the string.
  • if (isalpha(str[i])): Checks if the character is an alphabet letter.
  • str[j++] = str[i]: Copies the character to the new position.
  • str[j] = '\0': Marks the end of the filtered string.
  • int main(): Entry point of the program.
  • char str[100]: Declares a string array of size 100.
  • fgets(str, 100, stdin): Reads a line from the standard input.
  • removeNonAlpha(str): Calls the function to filter the string.
  • printf(...): Prints the filtered string result.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What function can you use to check if a character is an alphabet?

🎉Correct! The isalpha() function checks if a character is an alphabet letter.
🤔Not quite. Think about the function name that suggests alphabet checking.

Time and Space Complexity of the C Program

Here's how the program performs in terms of time and space complexity.

Time Complexity: O(n). n is the number of characters in the input string — such as 14 for EduSeekho 123!. The loop while (str[i] != '\0') checks each character once. For example, with 14 characters, the loop runs 14 times.

Space Complexity: O(1). The program uses a fixed set of variables like i, j, and str, relying only on these regardless of the input size.

⚠️

Common mistake: Not adding the null terminator '\0' after removing characters can lead to garbage values showing up.

Key takeaway: The isalpha() function does a great job of filtering out unwanted characters, keeping only the alphabets in your string.

FAQs About Removing Non-Alphabet Characters in C

Here are some frequently asked questions about this program:

isalpha() checks if a character's an alphabet letter. It helps filter out non-alphabet characters.
Yep, spaces are non-alphabet characters and they'll get removed.
The output will be empty since numbers aren't alphabetic characters.
You can tweak the condition to use isalnum(). That way, both letters and digits are included.
fgets() is better for reading strings because it avoids buffer overflow, unlike scanf().

If you found this program helpful, check out these related programs:

Conclusion

You can use this code to clean up strings in apps where you need tidy input, like data analytics or text cleaning tasks. Want more practice? Try switching the code to keep digits by using isalnum() instead of isalpha(). Check out other ways text processing can boost your C programs.

Test your skills further with our C programming quiz, and remember to try more code in the EduSeekho online compiler.