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

In this program, you will learn to remove all characters from a C-style string input 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)

C-strings in C++

  In C programming, the collection of characters is stored in the form of arrays. This is also supported in C++ programming. Hence it's called C-strings. C-strings are arrays of type char terminated with a null character, that is, \0 (ASCII value of null character is 0).

 How to define a C-string?
char str[] = "C++";
In the above code, str is a string and it holds 4 characters.
Although "C++" has 3 characters, the null character \0 is added to the end of the string automatically.

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

Here our program takes a C-style string input from the user and removes all the characters except alphabets.
Declare two C-strings; char line[100],astring[100] and an integer type variable j=0;
Ask the user to enter a string. Read the string entered by the user to the string array line.
To read the text containing blank space, the cin.get() function can be used. This function takes two arguments.
The first argument is the name of the string (address of the first element of the string) and the second argument is the maximum size of the array.
In the above program, line is the name of the string and 100 is the maximum size of the array.
cin.getline(line, 100);

Now the string entered by the user is stored in the string array 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 line[i] != '\0'
of the string. Increment the value of i for each iteration.
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 astring if it is an alphabet.
aString[j++] = line[i];
After completing the loop we can get the string free from characters rather than alphabets from the astring. 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 character type strings; char  line[100]; astring[100] and an integer                 type variable j=0;

Step 5: Ask the user to enter the string.

Step 6: get the string into the string array line using the function cin.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 string astring[100].

Step 9: Get the string free from characters that are non-alphabets from the string                           astring[100].

Step 10: Display the string;

Step 11: Exit.

C++ Source Code

                                          #include <iostream>
using namespace std;

int main() {
    char line[100], aString[100];
    int j = 0;
    cout << "Enter a string: ";
    cin.getline(line, 100);

    for(int i = 0; line[i] != '\0'; ++i)
    {
        if ((line[i] >= 'a' && line[i]<='z') || (line[i] >= 'A' && line[i]<='Z'))
        {
            aString[j++] = line[i]; 

        }
    }
    aString[j] = '\0';

    cout << "Output String: " << aString;    
    return 0;
}
                                      

OUTPUT

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