用下划线替换空格 [英] Replace space with an underscore
问题描述
到目前为止,我已经有了。
string space2underscore(string text)
{
for(int i = 0; i {
if(text [i] =='')
text [i] ='_';
}
return text;
}
大多数情况下,如果我做的像这样, / p>
string word =hello stackoverflow;
word = space2underscore(word);
cout<<字;
这将输出hello_stackoverflow,这正是我想要的。
但是,如果我要做像
cin>>字;
word = space2underscore(word);
cout<<字;
我会得到第一个单词hello。
有人知道这个问题吗?
问题是 cin >> word
只读取第一个单词。如果您想要像整个操作一样操作,您应该使用 std :: getline
。
例如:
std :: string s;
std :: getline(std :: cin,s);
s = space2underscore(s);
std :: cout<< s<< std :: endl;
此外,您可能要检查您实际上是否能够读取行。你可以这样做:
std :: string s;
if(std :: getline(std :: cin,s)){
s = space2underscore(s);
std :: cout<< s<< std :: endl;
}
注意,你可能以更清晰的方式写你的函数。我个人会这样写:
std :: string space2underscore(std :: string text){
(std :: string :: iterator it = text.begin(); it!= text.end(); ++ it){
if(* it ==''){
* it ='_';
}
}
return text;
}
或者对于奖励积分,使用 std :: transform
!
编辑:
如果你碰巧足够幸运能够使用c ++ 0x功能(我知道这是一个大if),你可以使用lambdas和 std :: transform
,这将产生一些非常简单的代码:
std :: string s = hello stackoverflow;
std :: transform(s.begin(),s.end(),s.begin(),[](char ch){
return ch ==''?'_':ch ;
});
std :: cout<< s<< std :: endl;
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?
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;
这篇关于用下划线替换空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!