Home > Backend Development > C++ > body text

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

PHPz
Release: 2023-08-27 10:21:05
forward
1240 people have browsed it

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);
Copy after login

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”
Copy after login

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”
Copy after login

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

Input: “ ”
Result: 
Copy after login

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;
}
Copy after login

Output

Aforapple,bforball
Copy after login
Copy after login

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;
}
Copy after login

Output

Aforapple,bforball
Copy after login
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!