Have another way to solve this solution? Contribute your code (and comments) through Disqus. Previous: Write a Python program to construct the following pattern, using a nested for loop. Next: Write a Python program to count the number of even and odd numbers from a series of numbers. In this video we will learn to reverse each words in a given string using C programming language. Example: If the entered string is Hello World Then we will get the following output olleH dlroW.
Active8 months ago
I have this
string s1 = 'My name is X Y Z'
and I want to reverse the order of the words so that s1 = 'Z Y X is name My'
. I can do it using an additional array. I thought hard but is it possible to do it inplace (without using additional data structures) and with the time complexity being O(n)?
47 Answers
12 next
Reverse the entire string, then reverse the letters of each individual word.
After the first pass the string will be
and after the second pass it will be
reverse the string and then, in a second pass, reverse each word...
in c#, completely in-place without additional arrays:
In Smalltalk:
I know noone cares about Smalltalk, but it's so beautiful to me.
You cannot do the reversal without at least some extra data structure. I think the smallest structure would be a single character as a buffer while you swap letters. It can still be considered 'in place', but it's not completely 'extra data structure free'.
Below is code implementing what Bill the Lizard describes:
What language?If PHP, you can explode on space, then pass the result to array_reverse.
If its not PHP, you'll have to do something slightly more complex like:
Anyway cookamunga provided good inline solution using python!
This is assuming all words are separated by spaces:
This is not perfect but it works for me right now. I don't know if it has O(n) running time btw (still studying it ^^) but it uses one additional array to fulfill the task.
It is probably not the best answer to your problem because i use a dest string to save the reversed version instead of replacing each words in the source string. The problem is that i use a local stack variable named buf to copy all the words in and i can not copy but into the source string as this would lead to a crash if the source string is const char * type.
But it was my first attempt to write s.th. like this :) Ok enough blablub. here is code:
We can insert the string in a stack and when we extract the words, they will be in reverse order.
This quick program works..not checks the corner cases though.
Most of these answers fail to account for leading and/or trailing spaces in the input string. Consider the case of
str=' Hello world'
... The simple algo of reversing the whole string and reversing individual words winds up flipping delimiters resulting in f(str) 'world Hello '
.The OP said 'I want to reverse the order of the words' and did not mention that leading and trailing spaces should also be flipped! So, although there are a ton of answers already, I'll provide a [hopefully] more correct one in C++:
Store Each word as a string in array then print from end
In Python, if you can't use [::-1] or reversed(), here is the simple way:
Printing words in reverse order of a given statement using C#:
Actually, the first answer:
does not work because it undoes in the second half of the loop the work it did in the first half. So, i < words.length/2 would work, but a clearer example is this:
Note: I am not familiar with the PHP syntax, and I have guessed incrementer and decrementer syntax since it seems to be similar to Perl.
In c, this is how you might do it, O(N) and only using O(1) data structures (i.e. a char).
In Java using an additional String (with StringBuilder):
In Java in-place:
Here is a C implementation that is doing the word reversing inlace, and it has
O(n)
complexity.c# solution to reverse words in a sentence
output:peaople the of heart the in lies beauty Press any key to continue . . .
Better version
Check my blog http://bamaracoulibaly.blogspot.co.uk/2012/04/19-reverse-order-of-words-in-text.html
Check my blog http://bamaracoulibaly.blogspot.co.uk/2012/04/19-reverse-order-of-words-in-text.html
I know there are several correct answers. Here is the one in C that I came up with.This is an implementation of the excepted answer. Time complexity is O(n) and no extra string is used.