Open In App

Java Program to Swap Corner Words and Reverse Middle Characters of a String

Last Updated : 31 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string containing n numbers of words. The task is to swap the corner words of the string and reverses all the middle characters of the string.

Input:  "Hello this is the GFG user"
Output: "user GFG eth si siht Hello"
Input:  "Hello Bye"
Output: "Bye Hello"

Methods:

  1. Using the concept of ASCII values
  2. Using the split() method

Method 1: Using the concept of ASCII values  

We handle the position of space between the words using ASCII values. The ASCII value of space is 32.

  1. Create two string variables and two pointers lets name as index and index1
  2. Iterate the first loop using the index address variable till the first space and store all the characters in a string variable named First.
  3. Iterate another loop from reverse order using the index1 address variable till the first space and store all the characters in another string variable name as last.
  4. Now, we have the address variables index1 and index that both point to the next starting and ending position of the middle characters of the given string.
  5. Using both pointers store all the characters in reverse order in a third-string variable named as middle.
  6. Print the last string then the middle string then the first string.

Example:

Java




// Java Program to Swap Corner Words and Reverse Middle
// Characters
 
// Importing utility classes
import java.util.*;
// Importing input output classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Method 1
    // To swap corners words
    static void swap(String m, int length)
    {
 
        // Declaring string variables to
        // store the first and last characters
        String first = "";
        String last = "";
 
        // Creating first address variable
        // Initially initializing with zero
        int index = 0;
 
        for (index = 0; index < length; ++index) {
 
            // Checking the space
            if (m.charAt(index) == 32) {
                break;
            }
 
            // Storing the last word in the last variable
            last += m.charAt(index);
        }
 
        // Now creating second address variable
        // Initially initializing with zero
        int index1 = 0;
 
        for (index1 = length - 1; index1 >= 0; --index1) {
            if (m.charAt(index1) == 32) {
                break;
            }
 
            // Storing the First word of the given string
            first = m.charAt(index1) + first;
        }
 
        String middle = "";
        for (int i = index1 - 1; i > index; --i) {
            if (m.charAt(i) == 32) {
                middle += " ";
            }
            else {
 
                // Storing all the middle words
                middle += m.charAt(i);
            }
        }
 
        // Print and display all the string variables
        System.out.print(first + " " + middle + " " + last);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Given custom input string
        String m = "Hello this is the GFG";
 
        // Calculating string length using length() method
        // and storing it in a variable
        int length = m.length();
 
        // Calling the method 1 to swap the words
        // for our custom input string
        swap(m, length);
    }
}


Output

GFG eht si siht Hello

Time Complexity: O(n) where n is the length of the string

Auxiliary Space: O(1)

Method 2: Using the split() method 

The string split() method breaks a given string around matches of the given regular expression.

Illustration:

Input        : 016-78967
Processing    : Regular Expression                               
Output       : {"016", "78967"}

Procedure: 

  • Store all the words in an array using split() method.
  • Swap the last and first element of the array.
  • Iterate the loop from the second position to the last second position.
  • Store all the characters in the reverse order name as the middle variable.
  • Print the first element of the array then the middle variable then the last element of the array.

Example: 

Java




// Java Program to Swap Corner Words and Reverse Middle
// Characters
 
// Importing utility classes
// Importing input output classes
import java.io.*;
import java.util.*;
 
// Main class
public class GFG {
 
    // Method 1
    // To swap the words in a string
    static void swap(String m, int length)
    {
        // Storing the words in the array
        String msg[] = m.split(" ");
 
        // Now swap the position of the first and the last
        // character in the string array
        String temp = msg[0];
        msg[0] = msg[msg.length - 1];
        msg[msg.length - 1] = temp;
 
        // Declaring and initializing string for
        // middle string empty middle string
        String mid = "";
 
        for (int i = msg.length - 2; i >= 1; --i) {
            String temp_s = msg[i];
 
            // Now storing the middle words in reverse order
            for (int j = temp_s.length() - 1; j >= 0; --j) {
                mid += temp_s.charAt(j);
            }
            mid += " ";
        }
 
        // Lastly print the swapped and reversed words
        System.out.print(msg[0] + " " + mid + " "
                         + msg[msg.length - 1]);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Custom input string
        String m = "Hello this is the GFG";
 
        // Calculating length using length() method and
        // storing it
        int length = m.length();
 
        // Calling the method 1 over custom input string to
        // get swapped corner words with reverse middle
        // characters
        swap(m, length);
    }
}


Output

GFG eht si siht  Hello

Time Complexity: O(n) where n is the length of the string

Auxiliary Space: O(n) where n is the length of the string



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads