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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:15:52+00:00 2026-05-13T08:15:52+00:00

Ok, i wrote 2 versions of this program. But im looking for the best

  • 0

Ok, i wrote 2 versions of this program. But im looking for the best solution – the most simple and fast one.

This is my solution but i was told that this solution is O(n*n) slower, which i dont know what really means. I was also told i could fasten it by breaking it into 2 functions, could anyone help me doing this?

void reverse3(char s[])
{
     int l;
     int i;

     l = strlen(s);
     if(l > 1)
     {
          reverse3(s + 1);

          for(i = 1; i < l; i++)
          {
              int temp = s[i-1];
              s[i-1] = s[i];
              s[i] = temp;
          }
     }
}
  • 1 1 Answer
  • 1 View
  • 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-13T08:15:52+00:00Added an answer on May 13, 2026 at 8:15 am

    You should swap the outermost bytes, i.e., i=1 and j=(l-1), then i=2, j=(l-2), etc. until i <= (j-1) (in other words, until either i=j, for an even number of characters, or i=j-1, for an odd number).

    That will perform in O(n).

    Recursion isn’t necessary, and “breaking it into two functions” doesn’t seem necessary either IMHO.

    Ok, read your edit about this being part of the assignment.

    In this case, you do need two functions to, as Jimmy said, preserve the parameter list of your “main” function.

    The second function would be something like:

    void swapRecursive(char[s], i, l) {      
       if(i >= (l-1) { return; } // no more swapping needed
       char t = s[i];
       s[i] = (l-i);
       s[l-i] = t;
       swapRecurse(s, i+1, l);
       }
    

    Please excuse my rusty C, I live in C#, VB.NET, and JavaScript now, some of the details escape me.

    Also, I may or may not have left a bug in there, since this is homework. 😉

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

Sidebar

Related Questions

I wrote this simple program today, but I found that cin.get() refuses to work
I wrote a program that uses builtin function bin(), but this function is new
I ran into this quite often stated problem but even after looking up nearly
I'm learning to work with lucene. I wrote a simple program to test lucene
I am using windows 7 and program in Netbeans platform. I have wrote this
I have a python program I wrote that I am trying to compile with
I feel so ashamed about this question, but I can't find a solution, and
I wrote a program that worked perfectly until the market required me to add
I have a small python program I wrote for a friend, one of the
I am sure there is a simple answer to this, but my google-fu fails

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.