fastread homefastrread library fastread menu

C++ : Remove all Characters in a String Except Alphabet

Tutorial by:Manisha Dubey      Date: 2016-06-18 04:58:57

❰ Previous Next ❱

This program takes a strings from user and removes all characters in that string except alphabets.

Source Code to Remove Characters in String Except Alphabets


#include<iostream>
#include<cstring>
using namespace std;
int main(){
    char line[150];
    int i,j;
    cout << "Enter a string: ";
    cin.getline(line, 150);
    for(i=0; line[i]!='\0'; ++i)
    {
        while (!((line[i]>='a'&&line[i]<='z') 
                || (line[i]>='A'&&line[i]<='Z' 
                || line[i]=='\0')))
        {
            for(j=i;line[j]!='\0';++j)
            {
                line[j]=line[j+1];
            }
            line[j]='\0';
        }
    }
    cout << "Output String: " << line;    
    return 0;
}

Output

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

 

 
 

This program takes a string from user and for loop executed until all characters of string is checked. If any character inside a string is not a alphabet, all characters after it including null character is shifted by 1 position backwards.

❰ Previous Next ❱


C++

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website