The Program in C++ program to remove all special characters from a given string is given below:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string removeSpecialCharacters(string str)
{
string result;
for (char c : str) {
if (isalnum(c)) { // Check if c is an alphanumeric character
result += c;
}
}
return result;
}
int main()
{
string str;
cout << "Hello Codeauri Family,Enter a string here to remove with special characters: ";
getline(cin, str);
cout << "Well, the String without special characters: " << removeSpecialCharacters(str) << endl;
return 0;
}
Output:
Hello Codeauri Family,Enter a string here to remove with special characters: Codeauri explains $peci@l characters!
Well, the String without special characters: Codeauriexplainspecilcharacters
Pro-Tips💡
This program uses the removeSpecialCharacters
function to remove all special characters from a given string str
.
The function uses a loop to iterate through each character in the string and checks if it is an alphanumeric character using the isalnum
function.
If a character is alphanumeric, it is added to the result string.
The main
function takes an input string from the user and calls the removeSpecialCharacters
function to remove the special characters, then outputs the result.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.