Swap corner words and reverse middle characters

Write a Java program to take an input string and exchange the first and last word and revers the middle word.

Examples:

Input : Hello World GFG Welcomes You
Output :You semocleW GFG dlroW Hello

Approach:

  • First we take two empty Strings and first String take the first word and second String takes the last word
  • When we iterate each word, then we must take care about the variable pointing to next word apart from the last word.
  • Now we reverse the left String in the given String.
  • After the above process, we first print last word and reverse of left Strings and after that the first word.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to illustrate the solution 
// of above problem
#include <iostream>
using namespace std;
  
void print(string s)
{
          
    // Taking an Empty String
    string fst = "";
    int i = 0;
    for (i = 0; i < s.length();) {
  
        // Iterating from starting index
        // When we get space, loop terminates
        while (s[i] != ' ') {
            fst = fst + s[i];
            i++;
        }
  
        // After getting one Word
        break;
    }
  
    // Taking an Empty String
    string last = "";
    int j = 0;
    for (j = s.length() - 1; j >= i;) {
  
        // Iterating from last index
        // When we get space, loop terminates
        while (s[j] != ' ') {
            last = s[j] + last;
            j--;
        }
  
        // After getting one Word
        break;
    }
  
    // Printing last word
    cout<<last;
    for (int m = j; m >= i; m--) {
  
        // Reversing the left characters
        cout<<s[m];
    }
  
    // Printing the first word
    cout<<fst;
}
  
int main() {
      
    string s = "Hello World GFG Welcomes You";
    print(s);
      
    return 0;
}
  
//This code is contributed by vt_m.

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate the solution of above problem
public class ExchangeFirstLastReverseMiddle {
    static void print(String s)
    {
        // Taking an Empty String
        String fst = "";
        int i = 0;
        for (i = 0; i < s.length();) {
  
            // Iterating from starting index
            // When we get space, loop terminates
            while (s.charAt(i) != ' ') {
                fst = fst + s.charAt(i);
                i++;
            }
  
            // After getting one Word
            break;
        }
  
        // Taking an Empty String
        String last = "";
        int j = 0;
        for (j = s.length() - 1; j >= i;) {
  
            // Iterating from last index
            // When we get space, loop terminates
            while (s.charAt(j) != ' ') {
                last = s.charAt(j) + last;
                j--;
            }
  
            // After getting one Word
            break;
        }
  
        // Printing last word
        System.out.print(last);
        for (int m = j; m >= i; m--) {
  
            // Reversing the left characters
            System.out.print(s.charAt(m));
        }
  
        // Printing the first word
        System.out.println(fst);
    }
  
    public static void main(String[] args)
    {
        String s = "Hello World GFG Welcomes You";
        print(s);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to illustrate the 
//solution of above problem
using System;
  
class ExchangeFirstLastReverseMiddle 
{
    static void print(string s)
    {
        // Taking an Empty String
        string fst = "";
        int i = 0;
        for (i = 0; i < s.Length;) {
  
            // Iterating from starting index
            // When we get space, loop terminates
            while (s[i] != ' '
            {
                fst = fst + s[i];
                i++;
            }
  
            // After getting one Word
            break;
        }
  
        // Taking an Empty String
        string last = "";
        int j = 0;
        for (j = s.Length - 1; j >= i;) {
  
            // Iterating from last index
            // When we get space, loop terminates
            while (s[j] != ' ') {
                last = s[j] + last;
                j--;
            }
  
            // After getting one Word
            break;
        }
  
        // Printing last word
        Console.Write(last);
        for (int m = j; m >= i; m--) {
  
            // Reversing the left characters
            Console.Write(s[m]);
        }
  
        // Printing the first word
        Console.Write(fst);
    }
  
    // Driver code
    public static void Main()
    {
        string s = "Hello World GFG Welcomes You";
        print(s);
    }
}
  
//This code is contributed by vt_m

chevron_right



Output:

You semocleW GFG dlroW Hello


My Personal Notes arrow_drop_up

Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator. I just love Programming languages and love to know new concepts everyday,every minute,every second. Here to help Other GEEKS!!!

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.