-1

I have been searching everywhere for an answer to this but I can only find the replace() function. That is great but I need to replace all the characters in the string with under scores except for spaces, not just one specific character.

This is for a hangman assignment.

Also I didn't provide any code because I don't need anything changed. I just need the syntax and logic help.

Sam R.
  • 33
  • 2
  • 1
    http://en.cppreference.com/w/cpp/algorithm/transform is a good start – vsoftco Nov 06 '15 at 04:24
  • Why is this getting down voted? I don't understand this community. I have a legitimate question and I receive no help. Why? – Sam R. Nov 06 '15 at 04:25
  • It's getting down-voted because it's not really clear how much effort you put in researching the issue. There are also countless of homework questions on SO that show ZERO effort, so this may be a reason. In any case, giving the benefit of the doubt, I posted an answer. – vsoftco Nov 06 '15 at 04:31
  • @vsoftco I understand the doubts in my efforts because of the amount of homework questions there are on here but I ask questions here as a last ditch effort to get an answer that I've been working on for hours. I appreciate your thoughtfulness. – Sam R. Nov 06 '15 at 04:35
  • Downvoters gotta downvote. This question has a (2, actually) short easy answer – Matt Timmermans Nov 06 '15 at 04:37

3 Answers3

1
string str("hang man");
for (auto it = str.begin(); it != str.end(); ++it)
{
    if (*it!=' ')
    {
        *it = '_';
    }
}
Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
  • I am getting a segmentation fault from this. Any ideas? We are just learning about pointers so I am not very good at debugging them. – Sam R. Nov 06 '15 at 05:06
  • Given the answer depends on C++11 (`auto`) anyway, might as well use C++11's new `for` loops: `for (char& c : str) if (c != ' ') c = '_';`. Still, good to see any clean `for` loop approach - easier for beginning C++ programmers to adapt this to many requirements, rather than getting bogged down in learning a bunch of fiddly declarative Standard Library functions. – Tony Delroy Nov 06 '15 at 05:34
1

Simple example of doing it with C++ standard library algorithms:

#include <algorithm>
#include <iostream>
#include <string>

int main() 
{
    std::string str = " this is a   test;?";
    std::transform(str.begin(), str.end(), str.begin(),
        [](char c){return  c != ' ' ? '_' : ' ';});

    // this also does it
    /* std::for_each(str.begin(), str.end(), 
        [](char& c){if(c != ' ') c = '_';});
    */

    std::cout << str;
}
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Thanks again, @vsoftco. – Sam R. Nov 06 '15 at 04:38
  • when I use this I get the errors: hangman.cpp:56: error: expected primary-expression before ‘[’ token hangman.cpp:56: error: expected primary-expression before ‘]’ token hangman.cpp:56: error: expected primary-expression before ‘char’ This is annoying that I can't format this well. Sorry. – Sam R. Nov 06 '15 at 04:49
  • Make sure you compile with C++11 support, flag `-std=c++11` for g++, as the code uses **lambda expressions**, which are new to C++11. – vsoftco Nov 06 '15 at 04:51
  • Oh I see. Thanks again! – Sam R. Nov 06 '15 at 04:52
  • I'm using putty and it says "unrecognized command line option "-std=c++11". – Sam R. Nov 06 '15 at 04:54
  • @SamR. Then it means the compiler is quite old, try `-std=c++0x` instead. If it doesn't work, then you can use [Matt's solution](http://stackoverflow.com/a/33559504/3093378), making sure you replace `auto` by `std::string::iterator`. Or define a [functor](http://stackoverflow.com/q/27256062/3093378) to replace the lambda. – vsoftco Nov 06 '15 at 04:56
1

Here's yet another alternative using the standard library algorithm std::replace_if. I like this solution because the name of the algorithm is meaningful and clearly says what it does.

#include <algorithm>
#include <iostream>
#include <string>

int main()
{
    std::string str = "I like turtles";

    std::replace_if(str.begin(), str.end(), [](char ch){ return ch != ' '; }, '_');

    std::cout << str << '\n';
}
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70