C++ program to remove all character from string except alphabets

Removing all characters from the string except alphabets.

Removing all characters excepts alphabets means removing special characters like ‘*’, ‘?’,  ‘@’,  etc. and numeric characters like ‘1’, ‘2’, ‘3’, etc. from the input string. So in this article we will learn how to code a C++ program to remove all characters from the string except alphabets. To do this first we will iterate each character of the string through a for loop then we will check if the character iterated is an alphabetic character or not. If it is found to be an alphabetical character than we will store it else we will not. In this way we will get a string that have only alphabetical characters as an output.

C++ program to remove all character from string except alphabets

Algorithm:

  • Initialize the variables.
  • Accept the input.
  • Initialize a for loop and terminate it at the end of string.
  • Iterate each character through the loop.
  • Remove non alphabetical characters using while loop.
  • Store only alphabetical characters from the string using another for loop
  • Print result.

C++ programming code to remove all characters from string except alphabets

Competitive Coding Techniques
#include <iostream>
using namespace std;
int main()
{
    //Initializing variable.
    char str[100];
    int i, j;
    
    //Accepting input.
    cout<<"Enter a string : ";
    gets(str);

    //Iterating each character and removing non alphabetical characters.
    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'; 
        }
    }
    //Printing output.
    cout<<"After removing non alphabetical characters the string is :";
    puts(str);
    return 0;
}
Output:
Enter a string : *1prep_insta*
After removing non alphabetical characters the string is :prepinsta

Operations on Strings

  • Length of the string without using strlen() function : C | C++ | Java | Python
  • Toggle each character in a string : C | C++ | Java | Python
  • Count the number of vowels : C | C++ | Java | Python
  • Remove the vowels from a String : C | C++ | Java | Python
  • Check if the given string is Palindrome or not : C | C++ | Java | Python
  • Print the given string in reverse order : C | C++ | Java | Python
  • Remove all characters from string except alphabets : C | C++ | Java | Python
  • Remove spaces from a string : C | C++ | Java | Python
  • Remove brackets from an algebraic expression : C | C++ | Java | Python
  • Count the sum of numbers in a string : C | C++ | Java | Python
  • Capitalize the first and last character of each word of a string : C | C++ | Java | Python
  • Calculate frequency of characters in a string : C | C++ | Java | Python
  • Find non-repeating characters in a string : C | C++ | JAVA | Python
  • Check if two strings are Anagram or not : C | C++ | Java | Python
  • Replace a sub-string in a string: C | C++ | Java | Python
  • Count common sub-sequence in two strings : C | C++ | Java | Python
  • Check if two strings match where one string contains wildcard characters : C | C++ | Java | Python
Operations on Strings

One comment on “C++ program to remove all character from string except alphabets”


  • Promit

    #include
    #include
    using namespace std;
    int main()
    {
    char a[100],b[100];
    int i=0,j=0,k=-1;
    gets(a);
    for(i=0;a[i]!=’\0′;i++)
    {

    if(a[i]>=’a’&&a[i]=’A’&&a[i]<='Z')
    {
    k++;
    a[k]=a[i];

    }
    else
    {
    continue;
    }
    }
    a[k+1]='\0';
    puts(a);

    }