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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:19:19+00:00 2026-05-31T02:19:19+00:00

#include <stdio.h> int main(void) { int i,j; int wordstart = -1; int wordend =

  • 0
#include <stdio.h>

int main(void)
{
  int i,j;
  int wordstart = -1;
  int wordend = -1;
  char words[]= "this is a test";
  char temp;

  // Reverse each word
  for (i = 0; i < strlen(words); ++i)
  {
    wordstart = -1;
    wordend = -1;
    if(words[i] != ' ') 
      wordstart = i;
    for (j = wordstart; j < strlen(words); ++j)
    {
      if(words[j] == ' ')
      {
        wordend = j - 1;
        break;
      }
    }
    if(wordend == -1)
      wordend = strlen(words);
    for (j = wordstart ; j <= (wordend - wordstart) / 2; ++j)
    {
      temp = words[j];
      words[j] = words[wordend - (j - wordstart)];
      words[wordend - (j - wordstart)] = temp;
    }
    i = wordend;
    printf("reversed string is %s:", words);
  }
}

I tried in this way but i am getting this output:
siht is a test
my expected output is:
test a is this

I would appreciate if some one could come with a different approach for which time complexity is very less or correct me if it is the right approach. Thanks

  • 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-31T02:19:20+00:00Added an answer on May 31, 2026 at 2:19 am

    Perhaps this belongs on the code review site instead?

    Your approach seems very efficient to me (except that I would only call strlen(words) once and save the result in a register).

    Two possible bugs look like:

    wordend = strlen(words);
    

    should be

    wordend = strlen(words)-1;
    

    and

    for(j = wordstart ; j <= (wordend - wordstart) / 2 ; ++j) {
    

    should be

    for(j = wordstart ; j <= (wordend + wordstart) / 2 ; ++j) {
    

    Final code looks like (with some extra {}):

        #include <stdio.h>
        int main(int argc,char *argv[])
        {
            int i,j;
            char words[]= "this is a test";
            int L=strlen(words);
    
            // Reverse each word
            for(i = 0; i < L; ++i) {
              int wordstart = -1;
              int wordend = -1;
              if(words[i] != ' ') 
              {
                wordstart = i;
    
                for(j = wordstart; j < L; ++j) {
                  if(words[j] == ' ') {
                    wordend = j - 1;
                    break;
                  }
                }
                if(wordend == -1)
                  wordend = L-1;
                for(j = wordstart ; j <= (wordend + wordstart) / 2 ; ++j) {
                  char temp = words[j];
                  words[j] = words[wordend - (j - wordstart)];
                  words[wordend - (j - wordstart)] = temp;
                }
                i = wordend;
              }
            }
            printf("reversed string is %s:",words);
            return 0;   
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain this behaviour? test.c: #include <stdio.h> int main(void) { printf(%d, %d\n, (int)
see this code #include<stdio.h> int main() { void test(void) { printf(test); return; } printf(main);
#include <stdio.h> #include <string.h> int main(void) { char *w; strcpy(w, Hello Word); printf(%s\n, w);
This is my code #include <stdio.h> void abc(char *text); int main(void) { char text[20];
consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
I got this C code. #include <stdio.h> int main(void) { int n, d, i;
#include stdio.h #include conio.h #include <iostream> using namespace std; int main (void) { char
#include <stdio.h> #include <string.h> int main(void) { char s[]= 9; printf(atoi = %d,atoi(s)); system(pause);
#include <stdio.h> int main(void){ char x [] = hello world.; printf(%s \n, &x[0]); return

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.