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

  • SEARCH
  • Home
  • 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 863439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:18:37+00:00 2026-05-15T09:18:37+00:00

So below I have a code in C++ that is supposed to invert the

  • 0

So below I have a code in C++ that is supposed to invert the arguments in a vector, but not the sequence. I have listed my problems as sidenotes in the code below. The invert function is supposed to invert each argument, and then the main function just outputs the inverted words in same order
For instance, program(“one two three four”)=ruof eerth owt eno

 #include <iostream>
 #include <string>
 using namespace std;

 int invert(string normal)
{
     string inverted;
     for (int num=normal.size()-1; num>=0; num--)
     {
         inverted.append(normal[num]);    //I don't know how to get each character
                                          //I need another command for append
     }
     return **inverted**;  <----
 }

 int main(int argc, char* argv[])
 {
     string text;
     for (int a=1; a<argc; a++)
     {
         text.append(invert(argv[a])); //Can't run the invert function
         text.append(" ");
     }
     cout << text << endl;
     return 0;
 }
  • 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-05-15T09:18:38+00:00Added an answer on May 15, 2026 at 9:18 am

    The problem is that normal[num] is a character. append() doesn’t have an overload for characters.

    You can use inverted.push_back(normal[num]);

    See the string API.

    I should add a couple other notes:

    0) Why are you returning int from invert()? inverted is a string, so you should return a string:

    `string invert(string normal);`
    

    1) Rather than using num to iterate, you can use reverse_iterators:

    for (string::reverse_iterator c = normal.rbegin(); c!=normal.rend(); ++c) {
      inverted.push_back(*c);
    }
    

    2) When passing strings to functions that do not get modified in the function, you should pass by reference to avoid extra string copies.

    string invert(const string &normal); 
    

    3) You’re traversing 0..argc, so your reversed strings will be backwards from what is expected, if I understand your requirements correctly.

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

Sidebar

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.