Answers for "C++ programming code to remove all characters from string except alphabets"

C++
0

C++ programming code to remove all characters from string except alphabets

#include 
using namespace std;
int main()
{
    //Initializing variable.
    char str[100];
    int i, j;
    
    //Accepting input.
    cout<<"Enter a string : ";
    gets(str);

    //Iterating each character and removing non alphabetical characters.
    for(i = 0; str[i] != ''; ++i)
    {
        while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '') )
        {
            for(j = i; str[j] != ''; ++j)
            {
                str[j] = str[j+1];
            }
            str[j] = ''; 
        }
    }
    //Printing output.
    cout<<"After removing non alphabetical characters the string is :";
    puts(str);
    return 0;
}
Posted by: Guest on February-08-2022
0

C++ programming code to remove all characters from string except alphabets

#include <bits/stdc++.h>
using namespace std;
// function to remove characters and
// print new string
void removeSpecialCharacter(string s) {
    for (int i = 0; i < s.size(); i++) {
        // Finding the character whose
        // ASCII value fall under this
        // range
        if (s[i] < 'A' || s[i] > 'Z' && s[i] < 'a' || s[i] > 'z') {
            // erase function to erase
            // the character
            s.erase(i, 1);
            i--;
        }
    }
    cout << s;
}
// driver code
int main() {
    string s = "P*re;p..ins't^a?";
    removeSpecialCharacter(s);
    return 0;
}
Posted by: Guest on February-08-2022

Code answers related to "C++ programming code to remove all characters from string except alphabets"

Browse Popular Code Answers by Language