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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:24:32+00:00 2026-06-17T17:24:32+00:00

I have seen this algorithm to reverse a string online, and I have some

  • 0

I have seen this algorithm to reverse a string online, and I have some doubts about it which I will specify at the end of the code:

    void reverseString(char *original_string)
    {
        char *end = original_string;
        char tmp;

        if(original_string) {
            while(*end) {
                ++end;
            }
            --end;
            while (original_string < end) {
                tmp = *original_string;
                *original_string++ = *end;
                *end-- = tmp;
            }
        }
//This line doesn't have the complete reversed string. Why?
printf("%s\n", original_string);
    }

1) In the while loop…Why do we compare two pointers? How do we know that the value is going to be bigger or smaller?? Those are just pointers, right?

2) Why don’t we return anything? Where is the reversed string? If we are impyling that the reversed string is in the original_string, shouldn’t we have used a pointer to pointer so that the effects are in the outter scope?

3) If I do the following:

char test[] = "hello";
    reverseString(test);
    printf("%s\n", test);

I can see the “olleh”. However, if I do printf("%s\n", original_string); at the very last line of the function reverseString, I just get “leh”. Why is that?

  • 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-17T17:24:33+00:00Added an answer on June 17, 2026 at 5:24 pm

    You have the two pointers pointing to the beginning and the end of the section left to be reversed. On each step you swap the characters pointed to by these pointers and then decrease the string that is left to be reversed from both directions – beginning and end. Comparing the pointers has the following meaning – until the beginning of the interval is not after the end of the interval, we still have an interval to reverse. Hope that makes sense.

    You don’t need to return anything – the string is reversed in place so the argument is in and out at the same time.

    3) In the function you take a copy of the pointer to the string, that you modify during its execution, so as the pointer is modified by the last line it will no longer point to the beginning of the string, but to the middle, where it is left at at the end.

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

Sidebar

Related Questions

I have seen this talked about but never answered. Maybe it has and I'm
I have seen this question and its answers and they clear up some of
i have seen this block of code in this Stackoverflow as a answer of
I have seen this question that talks about getting the last part of a
I have seen this symbol/operator in a block of code: a+=1; But I cannot
I have seen this question about deploying to WebSphere using the WAS ant tasks.
I have seen this code documented in the tk docs, but I can't figure
I have seen an algorithm for parallel merge-sort in a this paper. This is
In C++ I have only seen this done by converting the string object into
I have seen this error around the web a couple times and yet i

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.