Algorithm
1. Start
2. Initialize an empty string to store the result.
3. Iterate through each character in the input string.
a. Check if the character is an alphabet (using isalpha() function or other method).
b. If it is an alphabet, append it to the result string.
4. Output the result string.
5. End
Code Examples
#1 Code Example- C++ Programing Remove all characters except alphabets
Code -
C++ Programming
#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;
}
Copy The Code &
Try With Live Editor
Output
Output String: devsenv
#2 Code Example- Remove all characters except alphabets
Code -
C++ Programming
#include <iostream>
using namespace std;
int main() {
char line[100], alphabetString[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'))
{
alphabetString[j++] = line[i];
}
}
alphabetString[j] = '\0';
cout << "Output String: " << alphabetString;
return 0;
}
Copy The Code &
Try With Live Editor
Output
Output String: devsenv
Demonstration
C++ Programing Example to Remove all Characters in a String Except Alphabets-DevsEnv