Thread: Stripping a string of spaces

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    155

    Stripping a string of spaces

    How is this accomplished? I am using the string class ie

    string test;

    cin >> test; //they enter " This is a stri ng "

    *stripping process*

    //test now reads "Thisisastring"

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    If you must know I HAVE looked around for a few hours now and have attempted it myself multiple times; I am not just asking this without any effort

  3. #3
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    I'm not saying this is the best way to do this... I did an assignment like that a while ago and i used getline to input the string into an array. Then i just used a loop to copy every character that is not a space to a new array. Works fine. Don't forget the '\0' character at the end of the new string....
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    I tried this but it didn't work

    Code:
    string stripSpace(const string& infix){
    	string strippedString;
    
    	for (int i = 0; infix[i] != '\0'; i++){
    		if (infix[i] != ' ')
    			strippedString += infix[i];
    	}
    	return strippedString;
    }

  5. #5
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    I have never really worked with the string class before, so I can't really help you out there. If you want to use an array though, you could do it like this:

    [code]

    char rawinput[80];
    char input[80];

    cin.getline(rawinput, 80, '\n');

    for(int i = 0, j = 0;;i++) {
    if (rawinput[i] == ' ');
    else{
    input[j] = rawinput[i];
    j++;
    }
    if (rawinput[i] == '\0') break;
    }


    [\code]
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    I can do it with arrays of chars but it's the string class that's baffling me

  7. #7
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    Sorry then, i am doing a hw assignment with strings right now, and i am a bit baffled myself...
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    thank you, though

  9. #9
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Code:
    string::iterator iter = test.begin();
    string::iterator end = test.end();
    
    while(iter != end)
    {    if(*iter == ' ')
          {  test.erase(iter,iter+1);
          }
          iter++;
    }

  10. #10
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    Code:
    int main()
    {
       string str = "blah blah blah";
    
       cout<<str<<endl;
    
       int x = str.find(" ");
       while(x < str.length())
       {
          str.replace(x, 1, "");
          x = str.find(" ", x + 1);
       }
    
       cout<<str<<endl;
    
       return 0;
    }

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    I also realized something. I am not inputting my string correctly.

    cin >> string; takes to the first space

    cin.getline doesn't work

    Ahhhh!

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    But BTW thanks you guys, that DOES work for removing whitespace

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Thank all of you so much for the help!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. removing spaces from a string
    By bradleym83 in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2005, 01:59 PM
  4. Replies: 3
    Last Post: 04-06-2005, 11:04 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM