How to swap corner words and reverse middle characters of a string in java

A series of numbers n words. Its job is to swap the corner words of the string and reverse 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 process the position of spaces between words with ASCII values. The ASCII value of the space is 32.

  1. Create two string variables and two pointers allowing names like index and index1
  2. The first iterize loop uses the address index variable next to the first space and stores all characters in a string variable called First.
  3. Repeat another loop in reverse order, using the variable at index1 address next to the first space, and storing all characters in the variable name other than the last string.
  4. We now have the address variables index1 and index, both of which point to the next starting and ending positions of the middle character of the specified string.
  5. When both pointers are used, all characters are stored in reverse order in a third string variable called Middle.
  6. Print the last string, then the middle string, then the first string.

Example:

// 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

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: 

  1. Store all the words in an array using the split() method.
  2. Swap the last and first elements of the array.
  3. Repeat the cycle from the second position to the penultimate position.
  4. Store all characters in reverse order as middle variables.
  5. Print the first element of the array, then the middle variable, then the last element of the array.

Example: 

 
// 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);
	}
}

 

Submit Your Programming Assignment Details