Replace space with an underscore

邮差的信 提交于 2019-12-14 03:45:01

问题


I am trying to write something that will replace all the spaces in a string with an underscore.

What I have so far.

string space2underscore(string text)
{
    for(int i = 0; i < text.length(); i++)
    {
        if(text[i] == ' ')
            text[i] = '_';
    }
    return text;
}

For the most part this would work, if I was doing something like.

string word = "hello stackoverflow";
word = space2underscore(word);
cout << word;

That would output "hello_stackoverflow", which is just what I want.

However if I was to do something like

string word;
cin >> word;
word = space2underscore(word);
cout << word;

I would just get the first word, "hello".

Does anybody know a fix for this?


回答1:


The problem is that cin >> word is only going to read in the first word. If you want to operate on a whole like at a time, you should use std::getline.

For example:

std::string s;
std::getline(std::cin, s);
s = space2underscore(s);
std::cout << s << std::endl;

Also, you may want to check that you actually were able to read a line. You can do that like this:

std::string s;
if(std::getline(std::cin, s)) {
    s = space2underscore(s);
    std::cout << s << std::endl;
}

Finally, as a side note, you could probably write your function in a cleaner way. Personally I would write it like this:

std::string space2underscore(std::string text) {
    for(std::string::iterator it = text.begin(); it != text.end(); ++it) {
        if(*it == ' ') {
            *it = '_';
        }
    }
    return text;
}

Or for bonus points, use std::transform!

EDIT: If you happen to be lucky enough to be able to use c++0x features (and I know that's a big if) you could use lambdas and std::transform, which results in some very simple code:

std::string s = "hello stackoverflow";
std::transform(s.begin(), s.end(), s.begin(), [](char ch) {
    return ch == ' ' ? '_' : ch;
});
std::cout << s << std::endl;



回答2:


You've got your getline issue fixed but I just wanted to say the Standard Library contains a lot of useful functions. Instead of a hand-rolled loop you could do:

std::string space2underscore(std::string text)
{
    std::replace(text.begin(), text.end(), ' ', '_');
    return text;
}

This works, it's fast, and it actually expresses what you are doing.




回答3:


The problem is with your understanding of std::cin from the iostream library: Using the >> operator on a stream with an std::string as the right-hand-side argument only takes one word at a time (using whitespace to separate).

What you want instead is to use std::getline() to get your string.




回答4:


For a modern C++1x approach you have the option of std::regex_replace.

#include <regex>
#include <string>
#include <cstdlib>
#include <iostream>

using std::cout;
using std::endl;
using std::regex;
using std::string;
using std::regex_replace;

int main( const int, const char** )
{
   const auto target = regex{ " " };
   const auto replacement = string{ "_" };
   const auto value = string{ "hello stackoverflow" };

   cout << regex_replace( value, target, replacement ) << endl;

   return EXIT_SUCCESS;
}

Pros: Less code.

Cons: Regular expressions can cloud intent.




回答5:


Replace

cin >> word;

With

getline(cin, word);


来源:https://stackoverflow.com/questions/5252612/replace-space-with-an-underscore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!