1 / 2
Aug 2014

Hello ! I am having some problems when i try to make a problem. It consists in erasing all the multiple white spaces and just leaving 1 space for each word.
This is the text: HelloMyName______isDelshire._______ Take de '' as white spaces. I managed to get the 'inner' multiple spaces but i do not know how to erase the ones at the beginning and at the end. This is what i did:

#include <string>
#include <iostream>
using namespace std;
class Foo
{
public:
    void removeDoubleSpaces( string t ) 
    {  
      int i;
      i = t.find("  ");
      string ch;
      if ( i > -1 )
      {
        ch = t.replace( i,2," " );
        removeDoubleSpaces (ch); 
      }
      else if ( i == -1 )
      {
        cout << t << endl;    
      }  
    }
};      
int main()
{
   Foo bar;
   bar.removeDoubleSpaces("       Hi                              my   user   is    Delshire ,    what's yours?.              ");
   cin.get();
   return 0;
}

I need to make the beginning and the ending with no spaces, with my code i have 1 space and it should not be like that stuck_out_tongue

Thanks!

  • created

    Aug '14
  • last reply

    Aug '14
  • 1

    reply

  • 225

    views

  • 2

    users

You can simply check the first and last characters and remove them if they are spaces. (And there are other ways to solve this problem.)

If you plan on publishing this on SPOJ, note that you would need to use exact judge, otherwise a program that just echos the input directly to output would pass. Also note that it's not hard/interesting enough for classical section (but if it's for your own contest, of course you don't need to worry about that).

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 13 5d

Want to read more? Browse other topics in C and C++ or view latest topics.