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


January 21, 2023, Learn eTutorial
1254

Define string in C++.

A string can be defined as a sequence of characters. In C++ there are two types of strings which are,

  • Strings that are objects of the string class 
  • C-strings (C-style Strings)

C-strings in C++

In C programming, the collection of characters to form a string is stored in 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.

 How to write a C++ program to remove all characters except alphabets in a string?

This C++ program takes a C-style string input from the user. for that, declare two C-strings; char line[100], astring[100], and an integer type variable j=0. Then 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.

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 using a for loop. Get each character and perform the comparison with the alphabets using if condition, and add the element to the astring if it is an alphabet.

aString[j++] = line[i]; After the loop, Display the output string the contains only characters.

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. 

Step 8: 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