top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Reverse the words in a string without changing their order?

+6 votes
111 views

Say you are given "Hello World"
Output should be "olleH dlroW"

related to an answer for: Reverse the order of the words
posted Oct 30, 2013 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button
Example string "Hello World"
Start reading original string character by character and push it in a stack of characters and also mark start of the current word(start =0 for the first word). If reached space start popping characters from stack and copy them the start point marked, and update start(start = 6) with loop_counter + 1(loop_counter will be 5 at this time as space is at index 5).

Comments invited

3 Answers

0 votes

Python code

import re
text = "This is a text for testing !"

for m in re.finditer(r"\w+", text):
     text = text[0:m.start()] + text[m.start():m.end()][::-1] +text[m.end():]

print text
answer Oct 30, 2013 by Pankaj Agarwal
0 votes

See this simple program where I am managing the boundary and just reversing the characters within the boundary and it works

#include <stdio.h>

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

void reverseWords(char *s)
{
  char *word_begin = s;
  char *temp = s;

  while( *temp )
  {
    temp++;
    if (*temp == '\0')
    {
      return;
    }
    else if(*temp == ' ')
    {
      reverse(word_begin, temp-1);
      word_begin = temp+1;
    }
  }
}

main()
{
        char words[] = "this is a test";
        reverseWords(words);
        printf("%s\n", words);
}
answer Jul 4, 2014 by Tapesh Kulkarni
–1 vote

Try this its working fine in C using recursion

#include <stdio.h>

void reverse();

void main() {      
        printf("Please enter a sentence: ");
        reverse();
        getch();
}    

void reverse() {    
        char c;
        scanf("%c",&c);
        if(c != '\n') {      
            reverse();
            printf("%c",c);
        }   
}
answer Jul 4, 2014 by Gaurav Maurya
Similar Questions
+6 votes

Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it

0 votes

Suppose you are given "Luv you 123 luv me" the expected output is "vuL uoy 123 vul em"

+3 votes

Is it possible if yes can someone share the code in C?

+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.

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
...