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:
#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.
Output of the Remove all Characters Except Alphabets Program
When you run the program and enter a string, here's what happens:
Enter a string: EduSeekho 123!
Filtered string: EduSeekho
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.
- Read the input string from the user.
- Initialize two indices
iandjto 0. - Loop through the string until the null character is reached.
- Check if the current character is an alphabet using
isalpha(). - If yes, copy it to the new position in the string.
- Move to the next character in the input string.
- Continue this until the end of the string.
- 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])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 answerWhat function can you use to check if a character is an alphabet?
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:
Related Programs
If you found this program helpful, check out these related programs:
- C Program to Sort Elements in Lexicographical Order
- C Program to Count Vowels and Consonants in a String
- C Program to Check Whether a Character is an Alphabet
- C Program to Find ASCII Value of a Character
- C Program to Display Characters from A to Z Using Loop
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.