• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
June 21, 2022 |9.4K Views
C++ Program to Remove all Characters in a String Except Alphabets
Description
Discussion

In this video, we will see a C++ program to remove all characters except alphabets. For this, a string can process the string word-by-word by checking for letters and then removing the current character from the letter thereby only retaining alphabets.

Example:
Input string: V*id@e$oG!e#e%k)s
Output: VideoGeeks

Here we use approaches for Remove all Characters in a String Except Alphabets:
1. For loop with If-else Condition

Algorithm:

Step 1: The program starts by declaring the string, which is to be processed.
Step 2: It then declares the variable "a" which is used in the loop. The loop iterates through each character in the string and checks if it is an alphabet or not.
Step 3: If it is an alphabet, then the character is appended to a new string that will be created after the loop ends.
Step 4: Else, it will remove the character from the original string.
Step 5: A new line of code is added after each iteration to show that we are still inside this loop and not at its end.

Apart from that, we will see the time complexity of the code, that is O(n*n). But as erase() may take O(n) in the worst case.

C++ Program to Remove all characters other than alphabets from a string: https://www.geeksforgeeks.org/remove-characters-alphabets-string/

Read More