Java Program to Swap Corner Words and Reverse Middle Characters of a String
Last Updated :
31 Jul, 2022
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:
- Using the concept of ASCII values
- 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.
- Create two string variables and two pointers lets name as index and index1
- Iterate the first loop using the index address variable till the first space and store all the characters in a string variable named First.
- 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.
- 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.
- Using both pointers store all the characters in reverse order in a third-string variable named as middle.
- Print the last string then the middle string then the first string.
Example:
Java
import java.util.*;
import java.io.*;
public class GFG {
static void swap(String m, int length)
{
String first = "" ;
String last = "" ;
int index = 0 ;
for (index = 0 ; index < length; ++index) {
if (m.charAt(index) == 32 ) {
break ;
}
last += m.charAt(index);
}
int index1 = 0 ;
for (index1 = length - 1 ; index1 >= 0 ; --index1) {
if (m.charAt(index1) == 32 ) {
break ;
}
first = m.charAt(index1) + first;
}
String middle = "" ;
for ( int i = index1 - 1 ; i > index; --i) {
if (m.charAt(i) == 32 ) {
middle += " " ;
}
else {
middle += m.charAt(i);
}
}
System.out.print(first + " " + middle + " " + last);
}
public static void main(String[] args)
{
String m = "Hello this is the GFG" ;
int length = m.length();
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
import java.io.*;
import java.util.*;
public class GFG {
static void swap(String m, int length)
{
String msg[] = m.split( " " );
String temp = msg[ 0 ];
msg[ 0 ] = msg[msg.length - 1 ];
msg[msg.length - 1 ] = temp;
String mid = "" ;
for ( int i = msg.length - 2 ; i >= 1 ; --i) {
String temp_s = msg[i];
for ( int j = temp_s.length() - 1 ; j >= 0 ; --j) {
mid += temp_s.charAt(j);
}
mid += " " ;
}
System.out.print(msg[ 0 ] + " " + mid + " "
+ msg[msg.length - 1 ]);
}
public static void main(String[] args)
{
String m = "Hello this is the GFG" ;
int length = m.length();
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
Share your thoughts in the comments
Please Login to comment...