top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

How to Reverse words of a line?

+2 votes
149 views

Given a list of space separated words, reverse the order of the words. Each line of text contains L letters and W words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of consecutive words.
for example :

Input:
this is a test

Output:
test a is this

posted Mar 15, 2016 by Shahsikant Dwivedi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote
#include<stdio.h>

/* function prototype for utility function to
   reverse a string from begin to end */
void reverse(char *begin, char *end);

/* Function to reverse words */
void reverseWords(char *s)
{
    char *word_begin = s;
    char *temp = s; /* temp is for word boundry */

    /* STEP 1 of the above algorithm */
    while( *temp )
    {
        temp++;
        if (*temp == '\0')
        {
            reverse(word_begin, temp-1);
        }
        else if(*temp == ' ')
        {
            reverse(word_begin, temp-1);
            word_begin = temp+1;
        }
    } /* End of while */

    /* STEP 2 of the above algorithm */
    reverse(s, temp-1);
}

/*Function to reverse any sequence starting with pointer
  begin and ending with pointer end */

void reverse(char *begin, char *end)
{
    char temp;
    while (begin < end)
    {
        temp = *begin;
        *begin++ = *end;
        *end-- = temp;
    }
}

/* Driver function to test above functions */
int main()
{
    char s[] = "i like this program very much";
    char *temp = s;
    reverseWords(s);
    printf("%s", s);
    getchar();
    return 0;
}
answer Mar 16, 2016 by Ajay Kumar Topno
0 votes

Logic is simple first reverse the whole string followed by reverse each individual. Check the following code (tested one) -

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

main()
{
  char words[] = "this is a test for queryhome";
  int i, j;

  // Reverse the entire string
  for(i = 0; i < strlen(words) / 2; i++) {
    char temp = words[i];
    words[i] = words[strlen(words) - i - 1];
    words[strlen(words) - i - 1] = temp;
  }


  // Reverse each word
  for(i = 0; i < strlen(words); ++i) {
    int wordstart = -1;
    int 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) - 1;

      for(j = wordstart ; j <= wordstart + ((wordend - wordstart) / 2) ; j++) {
        char temp = words[j];
        words[j] = words[wordend - (j - wordstart)];
        words[wordend - (j - wordstart)] = temp;
      }

      i = wordend;
    }
  }

  printf("%s\n", words);
}
answer Mar 16, 2016 by Salil Agrawal
0 votes

simple way is call the reverse method in java

String string="I am a student of BHU";
String reverse = new StringBuffer(string);
reverse().toString();
answer Mar 16, 2016 by Babru Bhan
But this will reverse the complete string, whereas question was to reverse the string without reversing individual words.
for(i=strlen(str); i>=0; i--)
    {
       if(str[i-1]==' ' || str[i-1]==NULL )
       {
           for(j=i; str[j]!=' '; j++)
           {
               printf("%c",str[j]);
           }
       }
       printf(" ");
    }
So you like to put the complete answer, you may edit the answer by pressing edit button.
i figured it out in a new way..what about this one? in this logic i have tried to reach at the starting of each word (in a reverse manner )and than printed it.
here is my code:
#include <stdio.h>
#include<string.h>
int main(void) {
    int n,i,j;
    char *p,*t;
    int lenstr;
    char Line[1001];
    fflush(stdin);
    gets(Line);
    lenstr=strlen(Line);
    p=Line;
    for(i=lenstr-1;i>=0;i--)
      {
           if(*(p+i)!=' ' && i>0)
              continue;
          t=p+i;
          if(i!=0)
             {
               *t='\0';
              t++;
             }
          printf("%s ",t);
     }
    return 0;
}
thanks....this one is perfect
Similar Questions
0 votes

I am looking to reverse a string using binary search algorithm but no clue. Can someone share the algo and may be C/C++ code?

+2 votes

For
e.g 1. "hello how are you" - distance between "hello" and "you" is 3.
e.g 2. "hello how are hello you" - distance is 1
e.g 3. "you are hello" - distance is -1. Order of "hello" and "you" should be preserved.
e.g 4. "hello how are hello" - distance is -1 since "you" did not occur even once.

+4 votes

Given a dictionary of strings and another string find out if the string is an exact match to some words in the dictionary or varies at most in only one place of some word of the dictionary?

+1 vote

Given a string and dictionary of words, form a word by removing minimum number of characters.
Characters can be removed in-order only.

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...