tags:

views:

76

answers:

4

hello i am using this code to replace the while space but it never replaces all the white space can u please tell me where i am wrong

#include <iostream>
#include <string>
#include <boost/regex.hpp>
#include<boost/algorithm/string.hpp>
#include<boost/algorithm/string/replace.hpp>

using namespace std;

int main(void)
{
        string s4="Hai ad                     asdasd                       asdasd";
        cout << boost::algorithm::replace_all_copy(s4, "    ", " ");
}
A: 

If you want to completely eliminate the whitespace, the final argument should be the empty string.

     cout << boost::algorithm::replace_all_copy(s4, "    ", "");
Potatoswatter
ya thats correct silly ....
bingoa
If this answer solved your problem you can accept it so others can regard this question as solved and can see what they can do if they have the same problem. :)
Exa
@bingoa: … which is to say, click the green checkmark at the top left of your favorite answer.
Potatoswatter
+1  A: 

Hi Bingoa, your code replaces every occurence of 4 white spaces with a single white space. Since there are multiple occurences of the 4 white space pattern, you get multiple (single) white spaces in the output again.

I'm not sure what you are trying to achieve. But to erase all white spaces you better use erase_all_copy(). Or to erase all occurences of 4 white spaces, you should follow Potatoswatters advice.

#include <iostream>
#include <string>
#include<boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/erase.hpp>

using namespace std;

int main(void) {
        string s4="Hai ad                     asdasd                       asdasd";

        cout << boost::algorithm::erase_all_copy( s4, " " ) << endl;
        cout << boost::algorithm::replace_all_copy( s4, "    ", "") << endl;
}

Or is your goal to remove all white spaces between words, except for one?

Martin
+1  A: 

The stream operators remove extra white space automatically

#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>

using namespace std;

int main()  // Note: main(void) is not valid
{
    string s4="Hai ad                     asdasd                       asdasd";


    // Technique 1: Simple
    std::stringstream  s4Stream(s4);

    std::string s5;
    std::string word;
    while(s4Stream >> word)
    {
        s5.append(word).append(" ");
    }
    std::cout << s4 << "\n" << s5 << "\n";


    // Technique2: Using STL algorithms
    std::stringstream s4bStream(s4);
    std::stringstream s5bStream;

    std::copy(std::istream_iterator<std::string>(s4bStream),
              std::istream_iterator<std::string>(),
              std::ostream_iterator<std::string>(s5bStream," ")
             );

    std::string       s5b = s5bStream.str();
    std::cout << s4 << "\n" << s5b << "\n";
}
Martin York
+1: I would venture (even though it's not explicit) that the idea is to remove spurious white spaces while still keep the words apart.
Matthieu M.
@Matthieu M: As the above code does. Note (technique 1): the append(" "). Note (technique 2): std::ostream_iterator() takes two parameters a stream and a separator `s5bStream," "`.
Martin York
@Martin: which is precisely why I upvoted you :) my comment may have been unclear :/
Matthieu M.
A: 

If the idea is to replace any number of spaces with a single space, then the standard unique(_copy) algorithm can be tricked to do that:

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <boost/lambda/lambda.hpp>

std::string remove_excessive_spaces(const std::string& s)
{
     using namespace boost::lambda;
     std::string result;
     //consider consequtive chars equal only if they are both spaces.
     unique_copy(s.begin(), s.end(), back_inserter(result), _1 == ' ' && _2 == ' ');
     return result;
}

int main(void)
{
    std::string s4="Hai ad                     asdasd                       asdasd";
    std::cout << remove_excessive_spaces(s4);
}
UncleBens