Reverse String according to the number of words

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to reverse string 
// according to the number of words 
#include<stdio.h>
#include<string.h>
  
// Reverse the letters of the word
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
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 (str[wend] != ' ' && wend <= end)
            wend++;
        wend--;
          
        //Reverse the letter
        //of the words
        reverse(str, wstart, wend);
    }
}
  
// Driver Code
int main()
{
    char str[1000] = "Ashish Yadav Abhishek Rajput Sunil Pundir";
    reverseletter(str, 0, strlen(str)-1);
    printf("%s", str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to reverse string 
// according to the number of words 
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);
        System.out.printf("%s", String.valueOf(str));
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to reverse string 
// according to the number of words 
using 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

chevron_right


Output:

ridnuP tupjaR vadaY Ashish Abhishek Sunil


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji, 29AjayKumar



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.