Write a Program to Remove Spaces From a String

C++// C++ Program to remove spaces from a string #include <iostream> #include <string> using namespace std; string remove_spaces(string str) { string result = ""; for (char c : str) { if (c != ' ') { result += c; } } return result; } int main() { string str = "Gfg to the moon"; cout << "Without spaces: " << remove_spaces(str) << endl; return 0; }

Output

Without spaces: Gfgtothemoon

Comments

Leave a Reply

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