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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:06:54+00:00 2026-06-12T02:06:54+00:00

I usually understand recursions pretty well, but because I’m new to C functions like

  • 0

I usually understand recursions pretty well, but because I’m new to C functions like strcpy and pointers I couldn’t figure out how this recursion reverses a string:

char *reverse(char *string)
{
    if (strlen(string) <= 1)
        return string;

    else
    {
        char temp = *string;

        strcpy(string, reverse(string+1));

        *(string+strlen(string)) = temp;

        return string;
    }
}

The strcpy part seems a little bit complicated to me, and also what’s the purpose of this line:
*(string+strlen(string)) = temp;?

I realize that after flipping the string you need to add the character that was at the beginning to the end of the string, but I’m not sure I understand the logic behind this code.

  • 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-12T02:06:56+00:00Added an answer on June 12, 2026 at 2:06 am

    This code is extremely inefficient but what it does is:

    1. Save the original first character
    2. Recursively reverse the rest of the string (string+1 is a pointer to the second character in the string).
    3. Copy the rest of the (reversed) string one character to the left.
    4. Put the original first character at the end (*(string+strlen(string)) = temp;).

    The *(string+strlen(string)) = temp; is equivalent to string[strlen(string)] = temp; if that is easier to understand.

    I will not recommend using this code at all, since it is extremely inefficient — it copies the entire string (and measures its length twice) in every iteration, not to mention waste stack space.

    A much better implementation would be:

    void reverse(char *s) {
      char *e = s+strlen(s)-1;
      while (e > s) {
        char tmp = *s;
        *s=*e;
        *e=tmp;
        s++; e--:
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Usually I do strcpy but here is looking like I can't copy bigger-sized to
I'm quite new to javascript but i usually understand most of the scripts that
I understand that placement new calls are usually matched with explicit calls to the
I can usually understand the reason behind a compiler warning, but this one seems
I understand it is kind of bloated question, but.... learning new language, you first
I may not always understand every function in an API but usually I can
I understand typecasting...but only in retrospect. My process to figure out what requires typecasting
I understand this is usually to do with recursion, except I am pretty sure
I don't understand this issue. Because this usually worked till now. This method is
I understand the answer in R to repetitive things is usually apply() rather than

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.