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 6111919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:41:09+00:00 2026-05-23T14:41:09+00:00

Okay, so I’m trying to reverse a C style string in C++ , and

  • 0

Okay, so I’m trying to reverse a C style string in C++ , and I’m coming upon some weird output. Perhaps someone can shed some light?

Here is my code:

 int main(){    

    char str[] = "string";
    int strSize = sizeof(str)/sizeof(char);

    char str2[strSize];

    int n = strSize-1;
    int i =0;


    while (&str+n >= &str){ 
        str2[i] = *(str+n);         
        n--;
        i++;
    }

    int str2size = sizeof(str)/sizeof(char);
    int x;

    for(x=0;x<str2size;x++){
        cout << str2[x];
    }

}

The basic idea here is just making a pointer point to the end of the string, and then reading it in backwards into a new array using pointer arithmetic.

In this particular case, I get an output of: ” gnirts”
There is an annoying space at the beginning of any output which I’m assuming is the null character? But when I try to get rid of it by decrementing the strSize variable to exclude it, I end up with some other character on the opposite end of the string probably from another memory block.

Any ideas on how to avoid this? PS: (would you guys consider this a good idea of reversing a string?)

  • 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-23T14:41:10+00:00Added an answer on May 23, 2026 at 2:41 pm

    A valid string should be terminated by a null character. So you need to keep the null character in its original position (at the end of the string) and only reverse the non-null characters. So you would have something like this:

    str2[strSize - 1] = str[strSize - 1]; // Copy the null at the end of the string
    
    int n = strSize - 2; // Start from the penultimate character
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so im working on this php image upload system but for some reason
Okay, I've seen but haven't programmed in C# before. You can assume I'm competent
Okay, not sure what I'm doing here, other than it's not right. Trying to
Okay, I might confuse some people who might be able to answer this question,
Okay, in python one can do this: def foo(monkeys): def bar(monkey): #process and return
Okay, so I'm trying to create a list of he folders, and sub-folders and
okay, so I'm trying to get a directory of folders and sub folders, but
Okay, so I've got some C code to perform a mathematical operation which could,
Okay, I'm having some serious issues here. I'm new to this site, and new
Okay, I'm trying to make a quick little class to work as a sort

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.