I've been trying to figure what I'm doing wrong all day. I'm becoming discouraged. Can anyone help me out?

void stripSpace(string &str) {   
 int i,j,len=str.length();   
 for (i=0;j=0;i<len;i++;j++) 
    if (str[j]==' ') {      
      str.erase(j,1);      
      j--;     
    }
  str[j]='\0';
}

I just won't seem to stop until I get this fixed!!!! Any help would be appreciated. It compiles alright, but when I run the main program the string is not cout'd.

Thanks in advanced, Lamabot

Recommended Answers

I guess I did post a [URL="http://www.daniweb.com/techtalkforums/post326797-9.html"]similar code[/URL] in another thread.... ;)

Jump to Post

All 6 Replies

Re: Removing white spaces from a string 80 80

Funny it even compiled. How in the world is the compiler supposed to know which statement is the evaluation statement, and which are assignment statements? Try changing the loop (eg. while() loop), and then it will work properly.

By the way, placing an else() statement after the if() and then incrementing j in there would be far more efficient than deincrementing it inside the if() statement. (This would also solve your for() loop problem by only having one loop variable to deal with).

Re: Removing white spaces from a string 80 80

Funny it even compiled. How in the world is the compiler supposed to know which statement is the evaluation statement, and which are assignment statements? Try changing the loop (eg. while() loop), and then it will work properly.

By the way, placing an else() statement after the if() and then incrementing j in there would be far more efficient than deincrementing it inside the if() statement. (This would also solve your for() loop problem by only having one loop variable to deal with).

I believe that is valid enhanced for loop syntax...? Anyway, I tried the while loop and it, of course, worked. :lol:

Look at this:

void stripSpace(string &str) {     
         int i;  
         for (i=0;i<str.length;i++)     
              if (str[j]==' ')        
                  str.erase(j,1);      
       }

It works for me! So I guess.... It'll suffice. I can't believe I didn't realize sooner that every loop it checks the string length which gets smaller after every erase. Joe, I think I need a break from plumbing. :-| :cheesy:

Thanks much, LamaBot

Re: Removing white spaces from a string 80 80

I guess I did post a similar code in another thread.... ;)

commented: Caught my little slip... ;) +8
Re: Removing white spaces from a string 80 80

>I guess I did post a similar code in another thread....
Well, except that you used commas to separate the multiple assignments, which will actually work.

Re: Removing white spaces from a string 80 80

Because that is the way multiple assignments ought to be done in C or as a matter of C++. The syntax dictates it. Try doing multiple assignments like Lazaro's on a decent compiler and you end up getting your share of errors.

Re: Removing white spaces from a string 80 80

...
It works for me! So I guess.... It'll suffice. I can't believe I didn't realize sooner that every loop it checks the string length which gets smaller after every erase. Joe, I think I need a break from plumbing. :-| :cheesy:

Thanks much, LamaBot

I'm quoting myself because didn't write all the code. Just noticed that. Here is the code:

#include <iostream>
#include <string>

using namespace std;
void stripSpace(string &);
int main() {
    string test("HKLM\      Software\Microsfot\  Windows\Current Version\Run");
    stripSpace(test);
    cout << test << endl;
    return 0;
}
void stripSpace(string &str) {
    for (int i=0;i<str.length();i++) 
       if (str[i]==' ') {
         str.erase(i,1);
         i--;
        }
}

Thanks for the help guys, LamaBot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.