0

I have a program that receives an input and goes character through character to avoid white spaces. What I need to do now is get each one of those characters that aren't white spaces and stores them in a string as a word.

Someone told me that getline stores each character, it has memory:

Then create a while loop that has a (!= ' ')condition and then use the string.append('x') function to "add" each character to the string variable you've created until you have a word.

I understand the concept but I don't know how to actually do it.

baldemora
  • 72
  • 7
  • Couldn't you do this with a simple regex? i.e. 's/ //g' – Nick Jul 10 '13 at 20:53
  • Are you learning by osmosis? [Get a C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jesse Good Jul 10 '13 at 20:56
  • How do know when to stop reading characters? Is it when there is a carriage return? Do you want the program to turn this: "a b c d" into "abcd"? – bspikol Jul 10 '13 at 21:02
  • Break down the process into individual steps and then start from the smallest step and look up how to do that small step in c++. Then move on to the next step until you are finished. Or, you could do as Jesse Good suggested and read a C++ book. – PhiloEpisteme Jul 10 '13 at 21:57

1 Answers1

0

Here is a simple application that takes a string and filters out any spaces.

// reading a text file
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string input;
  stringstream filter;
  cout << "Enter a string \n";
  cin >> input;
  for(int i = 0; i<input.length(); i++){
      if(input.at(i)!=' '){ //Chech to see is it a space or not
          filter << input.at(i); //If not a space add to the stringstream filter
      }
  }
  string output = filter.str(); //Final string with no spaces

  return 0;
}