Write a C++ Program to Remove all Characters in a String Except Alphabets

C++ Program to Remove all Characters in a String Except Alphabets

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn C++ programming concepts. You can learn C++ language at your own speed and time. One can learn concepts of C++ language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

C++ Program to Remove all Characters in a String Except Alphabets
C++ Program to Remove all Characters in a String Except Alphabets

Write a C++ Program to Remove all Characters in a String Except Alphabets

PROGRAM:C++ Program to Remove all Characters in a String Except Alphabets

/* C++ Program to Remove all Characters in a String Except Alphabets*/
1: Remove all characters except alphabets

#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;
}

2: Remove all characters except alphabets

#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;
}

Leave a Reply

Your email address will not be published. Required fields are marked *