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:
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.
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:
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); } }
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.