I have a problem to understand what I am doing wrong with this code.
I have the string Line123. What I want to do is to make the output of this string to look like:

"Number1> Number2 Hello Number1> Number2"

So what I did is to take all blancspaces away before ">"

The output of the string instead look like this:
"Number1r2 Hello Number1"
....................................................

What I am doing in the code is while going through the string character for character is to look for the first character that is != " " in the for loop.
With this I think I will find a character that I save in FoundCharacter. I also save the index for this position.

Next is the ifstatement that looks if this found character is ">" and the first now current position in the string is a " ".
If this is true I will replace this area with a "".
Though something is wrong.

int Blocking = 0, FoundIndex = 0;
std::string FoundCharacter;
std::string Line123 = "Number1 > Number2 Hello Number1      > Number2";

for(int i = 0; i < Line123.length(); i++)
{
      Blocking = 0;
      for( int i5 = i; i5 < Line123.length(); i5++)
      {
	 if( Line123.substr(i5, 1) != " " && Blocking == 0)
	 {
	 FoundCharacter = Line123.substr(i5, 1);
	 FoundIndex = i5;
	 Blocking = 1;
	 }
       }
				 
                if( Line123.substr(i, 1) == " " && FoundCharacter == ">" )
                {
	           Line123.replace(i, FoundIndex, "" );
                }
			 
}

You might try something like the following, it removes all spaces that precede an '>'.

const std::string srch = " >";
std::string::size_type pos;

while(std::string::npos != (pos = Line123.find(srch)))
{
    Line123.erase(pos, 1);
}

I was trying to find out something like this. This is a clean solution.
I will use the approach with != ::npos.

You might try something like the following, it removes all spaces that precede an '>'.

const std::string srch = " >";
std::string::size_type pos;

while(std::string::npos != (pos = Line123.find(srch)))
{
    Line123.erase(pos, 1);
}
Be a part of the DaniWeb community

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