C Program to Remove all Characters in a String Except Alphabets

In this example, you will learn to remove all the characters from a string entered by the user except the alphabets.

To understand this example, you should have the knowledge of the following C programming topics:

Remove Characters in String Except Alphabets

#include <stdio.h>
int main() {
    char line[150];
    printf("Enter a string: ");
    for (int i = 0; line[i] != '\0'; ++i) {
        while (!((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') || line[i] == '\0')) {
            for (int j = i; line[j] != '\0'; ++j) {
                line[j] = line[j + 1];
            line[j] = '\0';
    printf("Output String: ");
    return 0;


Enter a string: p2'r-o@gram84iz./
Output String: programiz

This program takes a string input from the user and stores in the line variable. Then, a for loop is used to iterate over characters of the string.

If the character in a string is not an alphabet, it is removed from the string and the position of the remaining characters are shifted to the left by 1 position.