C++ Program to remove all characters from a string object except alphabets

In this program, you will learn to remove all characters from a string object except alphabets.

String in C++

A string is a collection of characters. There are two types of strings commonly used in C++ programming language:
•    Strings that are objects of string class (The Standard C++ Library string class)
•    C-strings (C-style Strings)
  In C++, you can create a string object for holding strings. Unlike using char arrays, string objects have no fixed length and can be extended as per your requirement.

C++ program to remove all characters except alphabets in a string.

Here our program takes a string object input from the user.
Declare two string objects; string line and string temp;
Ask the user to enter a string. Read the string entered by the user to the string object line.
Here in our program, we are using the function getline() to get the entered line of text by the user. getline() function takes the input stream as the first parameter which is cin and line as the location of the line to be stored.
Now the string entered by the user is stored in the string object line. We have to extract all the characters that are not alphabets from the string. For this, we have to compare each character of the string with the alphabets from a to z and A to Z.
For accessing each of the elements in the string we can use a for loop.
Initialize the variable i to 0. And check for the condition i< size of the string. Increment the value of i for each iteration.
for (int i = 0; i < line.size(); ++i)
Get each character and perform the comparison with the alphabets.
If conditional statement can be used to perform this comparison.
if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z'))
add the element to the object temp if it is an alphabet.
Temp= temp + line[i];
After completing the loop we can get the string free from characters rather than alphabets from the object temp. Display the output string.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare two string type objects; string line; string temp;

Step 5: Ask the user to enter the string.

Step 6: get the string into the object line using the function getline().

Step 7: Access the characters of the string from the beginning and compare each of the                 characters with alphabets a to z and A to Z. 

Step8:If it matches then store the character to the object temp.

Step 9: Get the string free from characters that are non-alphabets from the object temp.

Step 10: Display the string;

Step 11: Exit.

C++ Source Code

                                          #include <iostream>
using namespace std;

int main() {
    string line;
    string temp = "";

    cout << "Enter a string: ";
    getline(cin, line);

    for (int i = 0; i < line.size(); ++i) {
        if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {
            temp = temp + line[i];
        }
    }
    line = temp;
    cout << "Output String: " << line;
    return 0;
}
                                      

OUTPUT

Enter a string: l4ea@rne$$45tuto87ria**ls
Output String: learnetutorials