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

The Archive Base Latest Questions

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

I was practicing some programming problems and tried to code the popular reverse words

  • 0

I was practicing some programming problems and tried to code the popular “reverse words in a string” problem.

I tried to come up with my own code in C. I am able to partially get it right. That is, “hello world” becomes “world olleh”. I am wondering what the bug is here. I think somewhere I am creating an off by 1 bug.

As much as possible, I wanted to do it without using library functions. I searched here for this problem & found many solutions, but I’d like to know why my solution doesn’t work.

Here is the code:

#include <stdio.h>
#include <string.h>

void reverse(char*, int);

int main(int argc, char **argv)
{
    char st[]= "hello world";
    int len = strlen(st);
    int i=0,j=0;

    reverse(st,len-1); // Reverse the entire string. hello world => dlrow olleh

    while(st[j]){ //Loop till end of the string
        if ( *(st+j) == ' ' || *(st+j) == '\0' ) { //if you hit a blank space or the end of the string
            reverse(st+i,j-1); // reverse the string starting at position i till position before the blank space i.e j-1
            i=++j; //new i & j are 1 position to the right of old j
        }
        else {
            j++; //if a chacacter is found, move to next position
        }               
    }       

    printf("%s",st);
    return 0;
}

void reverse(char *s, int n)
{
    char *end = s+n; //end is a pointer to an address which is n addresses from the starting address
    char tmp;
    while (end>s)  //perform swap
    {
        tmp = *end;
        *end = *s;
        *s = tmp;
        end--;
        s++;
    }
}

Thank you!

UPDATE: Based on @Daniel Fischer’s answer, here is the correct implementation : http://ideone.com/TYw1k

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

    The problem is that

    while(st[j]){ //Loop till end of the string
        if ( *(st+j) == ' ' || *(st+j) == '\0' )
    

    the while condition prevents the loop being entered at the end of the string, so the last word doesn’t get reversed again.

    You can either make it an infinite loop, and add an

    if (st[j] == '\0') break;
    

    after the reversing, or reverse the last word after the while loop was left.

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

Sidebar

Related Questions

I was practicing the dynamic programming problem on SPOJ. But I have no idea
I'm practicing some simple 2D game programming, and came up with a theory that
I'm practicing programming in Assembly, making code C. I don't understand the conversion of
I am practicing with PHP and AJAX but I have some problems! I'm trying
Practicing some C++, I ran into some code from a book. The usage of
I was just practicing some coding and noticed that I was able to use
I'm practicing some examples on model-view programming and have a query why the colors
I have been practicing some old C++ problems to prepare for a few job
I'm practicing object oriented syntax in javascript and am having some problems. This is
So I was practicing some linked list problems and I keep getting mixed up

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.