remove all character except alphabets in c programming

In this example, we take string as input and remove all character except alphabets.

Remove all Characters in a String Except Alphabets In C Programming

	
#include <stdio.h>
int main()
{
    char str[100];
    int i, j;
    
    printf(" Enter a string :: ");
    gets(str);

    for(i = 0; str[i] != '\0'; ++i)
    {
        while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '\0') )
        {
            for(j = i; str[j] != '\0'; ++j)
            {
                str[j] = str[j+1];
            }
            str[j] = '\0'; 
        }
    }

    printf(" After removing non alphabetical characters the string is :");
    puts(str);
    return 0;
}
	

Output :

	
Enter a string :: a quick brown fox jumps over the lazy dog
After removing non alphabetical characters the string is :aquickbrownfoxjumpsoverthelazydog
	

Share your thoughts

Ask anything about this examples