Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8151957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:33:58+00:00 2026-06-06T15:33:58+00:00

There’s a simple string manipulation problem where you are required to reverse the words

  • 0

There’s a simple string manipulation problem where you are required to reverse the words of each line: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=121&page=show_problem&problem=424

So:

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

Would become:

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

Now, I’ve written a simple java solution which looks something like:

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":" "));
}

Because I am trying to learn c++, I’ve also tried writing a c++ solution which looks like:

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];
}

My questions are:

  1. The c++ solution returns “wrong answer”, whereas the java one is accepted, why?
  2. What improvements, if any, can be made to the c++ solution?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:34:00+00:00Added an answer on June 6, 2026 at 3:34 pm
    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

    If this doesn’t pass, it’s probably because of the trailing space at the end of each line. Use a Boolean variable to avoid printing space before newline.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are two major fix engines: opensource QuickFix http://www.quickfixengine.org/ commercial Fix Antenna http://www.b2bits.com/trading_solutions/fix_engines/fix_engine_cpp.html What
There must be a simple solution to this problem but for the love of
There are many string matching algorithms can be used to find a pattern (string)
There's a bug in FireBug: I accidentally clicked where the line numbers are, which
There was some progress made to the earlier problem. Now there is a new
There must be a simple solution to this, but after 4 hours of browsing
There seem to be very different opinions about using transactions for reading from a
There seems to be a similar question to this on here but with the
There are many conflicting statements around. What is the best way to get the
There are load balanced tomcat web servers. Every request could be served by different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.