C plus plus code to remove special characters from a string [C++]

Below is the C plus plus source code to remove special characters other than alphabets (excluding upper and lower case letters).

// C++ source code to remove special characters other than alphabets from a string
#include <iostream>
using namespace std;

int main() 
{
    string str;
    
    cout << "Enter a string with special characters: ";
    // Read the entered string
    getline(cin, str);
    // remove special characters from the string
    for(int i = 0; i < str.size(); ++i)
    {
        if (!((str[i] >= 'a' && str[i]<='z') || (str[i] >= 'A' && str[i]<='Z')))
        {
            str[i] = '\0';
        }
    }
    // Print the result
    cout << "Output String after removing special characters is: " << str;    
    return 0;
}

Output:

cpp-code-output-to-remove-special-characters-from-string

C plus plus code output for removing special characters from string

 

Check C plus plus code to Extract String between two delimeters

About the Author

SRINI S

A passionate blogger. Love to share solutions and best practices on wordpress hosting, issues and fixes, excel VBA macros and other apps

Leave a Reply

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