c ++基本字符串操作

时间:2012-06-22 04:45:20

标签: java c++ string

有一个简单的字符串操作问题,您需要反转每行的单词:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=121&page=show_problem&problem=424

所以:

I love you.
You love me.
We're a happy family.

会变成:

I evol .uoy
uoY evol .em
er'eW a yppah .ylimaf

现在,我编写了一个简单的java解决方案,它看起来像:

BufferedReader file = new BufferedReader(new InputStreamReader(System.in));
String s;
while((s=file.readLine())!=null){
    String[] sr = s.split(" ");
    for(int i = 0; i<sr.length; i++)
        System.out.print(new StringBuffer(sr[i]).reverse() + (i==sr.length-1?"\n":" "));
}

因为我正在尝试学习c ++,所以我也尝试编写一个看起来像这样的c ++解决方案:

string s;
while(getline(cin, s)){
    string tmp = "";
    for(int i = 0; i<=s.length(); i++)
        if( i==s.length() || s[i] == ' '){
            for(int j = tmp.length(); j>=0; j--)
                cout << tmp[j];
            if( i == s.length()) cout << endl;
            else cout << " ";
            tmp = "";
        }else
            tmp += s[i];
}

我的问题是:

  1. c ++解决方案返回“错误答案”,而java文件被接受,为什么?
  2. 可以对c ++解决方案进行哪些改进?

6 个答案:

答案 0 :(得分:3)

std::string line_string;
while ( getline( std::cin, line_string ) ) {

    // Instead of `split`, read out of a std::istringstream:
    std::istringstream line_stream( line_string );
    std::string word;
    while ( line_stream >> word ) {

        // Use std::reverse instead of a loop:
        std::reverse( word.begin(), word.end() );

        // Always qualify with std:: instead of using namespace std;
        std::cout << word << ' ';
    }
    std::cout << '\n'; // prefer '\n' to std::endl unless you need a flush
}

http://ideone.com/hd3bg

如果没有通过,可能是因为每行末尾的尾随空格。使用布尔变量来避免在换行前打印空间。

答案 1 :(得分:3)

如果允许提升,我会使用boost::splitboost::joinstd::reverse的组合。

std::string line;
std::vector<std::string> vs;
while (getline(std::cin, line)) {       
    boost::split(vs, line, ::isspace);
    for (auto & word : vs)
        std::reverse(word.begin(), word.end());
    std::cout << boost::join(vs, " ") << '\n';
}

如果没有提升,我会在我的个人图书馆中拥有这些功能(或类似功能),我会将它们复制到我的提交中。

答案 2 :(得分:1)

错误的原因是:

string s;
while(getline(cin, s)){
    string tmp = "";
    for(int i = 0; i<=s.length(); i++)
        if( i==s.length() || s[i] == ' '){
            for(int j = tmp.length(); j>=0; j--)
                        ^^^^^^^^^^^^ => the first tmp[j] is \0!
                                     => should be tmp.length()-1
                cout << tmp[j];
            if( i == s.length()) cout << endl;
            else cout << " ";
            tmp = "";
        }else
            tmp += s[i];
}

请记住,在c / c ++中,索引从0开始。

答案 3 :(得分:1)

如果您绝对想要创建一个新变量来处理反向操作而不是以相反的顺序进行简单打印。然后你必须考虑到以下情况: 在C ++中,索引不是字符串的长度。因为索引从0开始,所以你从索引0开始到索引:length - 1

如果你想要开始使用C ++,在熟悉基础知识之后,最好先去STL,然后去其他地方(这只是我的观点)。

以下是一些代码:

string s;  
while(getline(cin, s))
{      
   string tmp = "";      
   for(int i = s.length() - 1; i >= 0; i++)
       std::cout<<s[i];
}

答案 4 :(得分:0)

我想如果我要这样做,我会通过构造一个新字符串来反转每个单词,并将word.rbegin(), word.rend()作为构造函数的参数传递。

使用我以前的一个答案中的Line class和另一个答案中的infix_ostream_iterator,它看起来像这样:

#include "infix_iterator.h"
#include "line"


int main() {
    auto rev_words = [](std::string const &in) -> std::string {
        std::istringstream b(in);
        std::ostringstream r;

        std::transform(std::istream_iterator<std::string>(b),
            std::istream_iterator<std::string>(),
            infix_ostream_iterator<std::string>(r, " "),
            [](std::string const &in){return std::string(in.rbegin(), 
                                                         in.rend());});
        return r.str();
    };

    std::transform(std::istream_iterator<line>(std::cin), 
        std::istream_iterator<line>(), 
        std::ostream_iterator<std::string>(std::cout, "\n"),
        rev_words);
    return 0;
}

是的,这可能在全功能风格上有点落伍。毫无疑问,做这样的事情会更容易:

std::string line;
while (std::getline(std::cin, line)) {
    std::istringstream buffer(line);
    std::transform(std::istream_iterator<std::string>(buffer),
                   std::istream_iterator<std::string>(),
                   infix_ostream_iterator<std::string>(std::cout , " "), 
                   [](std::string const &in){return std::string(in.rbegin(), 
                                                                in.rend()););
    std::cout << "\n";
}

答案 5 :(得分:0)

这是我试过的那个。

#include <iostream>
#include <string>

 using namespace std;


 int main(){

    string s = "Hello How are you Mr vins what are you doing now";
    string temp, reverse;

   int i,j;
     // Traverse through First string
    for (i=0;i <=s.length(); i++)
   {
if(s[i] == ' ' || s[i] == '\0') {
    //After getting space reverse the previous word and add to reverse string
    for (j = temp.length(); j >=0; j--)
        reverse = reverse + temp[j];

    reverse = reverse + " ";
    temp="";
    continue;
}
temp = temp + s[i];

}

 cout << "\nlets see reversed sentence \n";

   cout << reverse << "\n";
      return 0;
   }