Home  >  Article  >  Backend Development  >  C++ program to remove spaces from string using string stream

C++ program to remove spaces from string using string stream

PHPz
PHPzforward
2023-08-27 10:21:051214browse

C++ program to remove spaces from string using string stream

As mentioned in the given question, we need to remove spaces from string using string stream. As the name suggests, string streams convert strings into streams. It works like cin in C. It is associated with a string object that has access to the string buffer in which it is stored.

string s =" a for apple, b for ball";
res = solve(s);

Using a string buffer, we will read each word one by one and concatenate it into a new string, which will be our answer.

Note - String-like streams are available in C's sstream header, so we need to include it.

Let’s look at some input/output scenarios

Assuming there are no spaces in the input of the function, the output result will be the same as the input -

Input: “Tutorialspoint”
Result: “Tutorialspoint”

Assuming there are no spaces in the input of the function, the output result will be a string without spaces -

Input: “Tutorials Point”
Result: “TutorialsPoint”

Assuming that the input accepted by the function only contains spaces, this method cannot provide output results -

Input: “ ”
Result: 

algorithm

  • Consider an input string with characters.

  • Checks if the string is empty and removes any whitespace present in the input using the stringstream keyword.

  • This process will be completed until the string stream pointer reaches the end of the line.

  • If the end of the line of the string is reached, the program terminates.

  • The updated string is returned to the output result.

Example

For example, we have a string such as "a represents apple, b represents ball", and we need to convert it to "aforapple,bforball".

Follow the detailed code to remove spaces from a string input to make it a stream of characters -

#include <iostream>
#include <sstream>
using namespace std;
string solve(string s) {
   string answer = "", temp;
   stringstream ss;
   ss << s;
   while(!ss.eof()) {
      ss >> temp;
      answer+=temp;
   }
   return answer;
}
int main() {
   string s ="a for apple, b for ball";
   cout << solve(s);
   return 0;
}

Output

Aforapple,bforball

Example (using getline)

We have another way to solve the same query in C using getline.

#include <iostream>
#include <sstream>
using namespace std;
string solve(string s) {
   stringstream ss(s);
   string temp;
   s = "";
   while (getline(ss, temp, ' ')) {
      s = s + temp;
   }
   return s;
}
int main() {
   string s ="a for apple, b for ball";
   cout << solve(s);
   return 0;
}

Output

Aforapple,bforball

in conclusion

We see that using string streams, the strings are stored in a buffer and we can get the strings verbatim and concatenate them, removing spaces.

The above is the detailed content of C++ program to remove spaces from string using string stream. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete