Reverse String according to the number of words

16 Jun, 2022
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 
C++
C
Java
Python3
C#
Javascript
// C++ program to reverse string// according to the number of words#include <bits/stdc++.h>using namespace std; // Reverse the letters of the wordvoid reverse(char str[], int start, int end){     // Temporary variable to store character    char temp;    while (start <= end)    {        // Swapping the first and last character        temp = str[start];        str[start] = str[end];        str[end] = temp;        start++;        end--;    }} // This function forms the required stringvoid reverseletter(char str[], int start, int end){    int wstart, wend;    for (wstart = wend = start; wend < end; wend++)    {        if (str[wend] == ' ')            continue;                     // Checking the number of words        // present in string to reverse        while (str[wend] != ' ' && wend <= end)            wend++;        wend--;                 // Reverse the letter        // of the words        reverse(str, wstart, wend);    }} // Driver Codeint main(){    char str[1000] = "Ashish Yadav Abhishek Rajput Sunil Pundir";    reverseletter(str, 0, strlen(str) - 1);    cout << str;    return 0;} // This code is contributed by SHUBHAMSINGH10
// C program to reverse string// according to the number of words#include<stdio.h>#include<string.h> // Reverse the letters of the wordvoid reverse(char str[], int start, int end) {     // Temporary variable to store character    char temp;    while (start <= end)    {        // Swapping the first and last character        temp = str[start];        str[start] = str[end];        str[end] = temp;        start++;        end--;    }} // This function forms the required stringvoid reverseletter(char str[], int start, int end) {                 int wstart, wend;    for (wstart = wend = start; wend < end; wend++) {                             if (str[wend] == ' ')            continue;                     // Checking the number of words        // present in string to reverse        while (str[wend] != ' ' && wend <= end)            wend++;        wend--;                 //Reverse the letter        //of the words        reverse(str, wstart, wend);    }} // Driver Codeint main(){    char str[1000] = "Ashish Yadav Abhishek Rajput Sunil Pundir";    reverseletter(str, 0, strlen(str)-1);    printf("%s", str);    return 0;}
// Java program to reverse string// according to the number of wordsclass GFG{     // Reverse the letters of the word    static void reverse(char str[],                       int start, int end)    {         // Temporary variable to store character        char temp;        while (start <= end)        {            // Swapping the first and last character            temp = str[start];            str[start] = str[end];            str[end] = temp;            start++;            end--;        }    }     // This function forms the required string    static void reverseletter(char str[],                            int start, int end)    {         int wstart, wend;        for (wstart = wend = start; wend < end; wend++)        {             if (str[wend] == ' ')            {                continue;            }             // Checking the number of words            // present in string to reverse            while (wend <= end && str[wend] != ' ')            {                wend++;            }            wend--;             // Reverse the letter            // of the words            reverse(str, wstart, wend);        }    }     // Driver Code    public static void main(String[] args)    {        char str[] = "Ashish Yadav Abhishek Rajput Sunil Pundir".toCharArray();        reverseletter(str, 0, str.length - 1);        System.out.printf("%s", String.valueOf(str));    }} // This code contributed by Rajput-Ji
# Python3 program to reverse string# according to the number of words # Reverse the letters of the worddef reverse(string, start, end):     # Temporary variable to store character    temp = ''    while start <= end:         # Swapping the first and last character        temp = string[start]        string[start] = string[end]        string[end] = temp        start += 1        end -= 1 # This function forms the required stringdef reverseletter(string, start, end):    wstart, wend = start, start     while wend < end:        if string[wend] == " ":            wend += 1            continue         # Checking the number of words        # present in string to reverse        while wend <= end and string[wend] != " ":            wend += 1        wend -= 1         # Reverse the letter        # of the words        reverse(string, wstart, wend)        wend += 1 # Driver Codeif __name__ == "__main__":    string = "Ashish Yadav Abhishek Rajput Sunil Pundir"    string = list(string)    reverseletter(string, 0, len(string) - 1)    print(''.join(string)) # This code is contributed by# sanjeev2552
// C# program to reverse string// according to the number of wordsusing System;     class GFG{      // Reverse the letters of the word    static void reverse(char []str,                       int start, int end)    {          // Temporary variable to store character        char temp;        while (start <= end)        {            // Swapping the first and last character            temp = str[start];            str[start] = str[end];            str[end] = temp;            start++;            end--;        }    }      // This function forms the required string    static void reverseletter(char []str,                            int start, int end)    {          int wstart, wend;        for (wstart = wend = start; wend < end; wend++)        {              if (str[wend] == ' ')            {                continue;            }              // Checking the number of words            // present in string to reverse            while (wend <= end && str[wend] != ' ')            {                wend++;            }            wend--;              // Reverse the letter            // of the words            reverse(str, wstart, wend);        }    }      // Driver Code    public static void Main(String[] args)    {        char []str = "Ashish Yadav Abhishek Rajput Sunil Pundir".ToCharArray();        reverseletter(str, 0, str.Length - 1);        Console.Write("{0}", String.Join("",str));    }} // This code has been contributed by 29AjayKumar
<script>// Javascript program to reverse string// according to the number of words // Reverse the letters of the wordfunction reverse(str,start,end){    // Temporary variable to store character        let temp;        while (start <= end)        {            // Swapping the first and last character            temp = str[start];            str[start] = str[end];            str[end] = temp;            start++;            end--;        }} // This function forms the required stringfunction reverseletter(str,start,end){    let wstart, wend;        for (wstart = wend = start; wend < end; wend++)        {               if (str[wend] == ' ')            {                continue;            }               // Checking the number of words            // present in string to reverse            while (wend <= end && str[wend] != ' ')            {                wend++;            }            wend--;               // Reverse the letter            // of the words            reverse(str, wstart, wend);        }}  // Driver Codelet str= "Ashish Yadav Abhishek Rajput Sunil Pundir".split("");reverseletter(str, 0, str.length - 1);document.write((str).join("")); // This code is contributed by rag2127</script>
ridnuP tupjaR vadaY Ashish Abhishek Sunil
 
Time complexity : O(n) Auxiliary Space : O(1)
Rajput-Ji
29AjayKumar
sanjeev2552
SHUBHAMSINGH10
rag2127
surinderdawra388
a7977370173
Reverse
Strings
Strings
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Longest Palindromic Substring | Set 1
Length of the longest substring without repeating characters
Convert string to char array in C++
Top 50 String Coding Problems for Interviews
What is Data Structure: Types, Classifications and Applications
Check whether two strings are anagram of each other
Print all the duplicates in the input string