Given a string containing a number of words. If the count of words in string is even then reverse its even position’s words else reverse its odd position, push reversed words at the starting of a new string and append the remaining words as it is in order.
Examples:
Input: Ashish Yadav Abhishek Rajput Sunil Pundir Output: ridnuP tupjaR vadaY Ashish Abhishek Sunil Input: Ashish Yadav Abhishek Rajput Sunil Pundir Prem Output: merP linuS kehsihbA hsihsA Yadav Rajput Pundir
Approach : If number of words are even then even position’s words come first and also reverse that particular word, and if number of words are odd then odd position’s words come first and also reverse that particular word, after then the remaining words are appended in order. For e.g.Ashish Yadav Abhishek Rajput Sunil Pundir. In the above string, the number of words is even then “Yadav Rajput Pundir” comes at the even position and then the final output will be: ridnuP tupjaR vadaY Ashish Abhishek Sunil
0 Comments