Javatpoint Logo
Javatpoint Logo

Swap Corner Words and Reverse Middle Characters in Java

Swapping corner words and reversing middle characters in Java exemplify a creative approach to string manipulation, a fundamental programming aspect. The task involves altering the positions of the first and last words in a string while inverting the order of the characters nestled between them.

Example 1:

Input: "Java programming language tutorial"

Output: "tutorial egaugnal gnimmargorp Java"

Example 2:

Input: "Data structures and algorithms"

Output: "algorithms dna serutcurts Data"

Example 3:

Input: "Machine learning is fascinating"

Output: "fascinating si gninrael Machine"

Approach: Using ASCII values

The Java code for swapping corner words and reversing a string's middle characters leverages Java's string manipulation capabilities, not ASCII values. It utilizes character traversal and string concatenation to effect changes, showcasing a straightforward approach over low-level ASCII manipulations for text processing. The method underscores the efficiency of using built-in string functions for common string operations.

Algorithm:

Step 1: Input a string inputString and calculate its length stringLength.

Step 2: Initialize two empty strings, firstWord and lastWord, for the corner words.

Step 3: Use a loop to identify lastWord by traversing from the start until the first space is encountered.

Step 4: Use another loop to identify firstWord by traversing from the end backwards until the first space is encountered from that end.

Step 5: Initialize an empty string middleReversed for the reversed middle section.

Step 6: Reverse the middle section (excluding the first and last words) by traversing it backward and appending each character to middleReversed. Include spaces as they are encountered.

Step 7: Concatenate firstWord, middleReversed, and lastWord with spaces between them to form the final String.

Implementation:

Filename: StringSwapper.java

Output:

Swapped and Reversed String: tutorial egaugnal gnimmargorp Java

Time Complexity:

O(n): Linear, due to operations that scale with the input string's length.

Auxiliary Space:

O(1): Considered constant, focusing on the algorithm's inherent logic rather than the space for processed data.

Approach: Using the split() method

The approach used in the split() method from the String class for manipulating a given string by swapping its corner words and reversing the characters in the middle section.

Algorithm:

Step 1: Define a method performSwapAndReverse that takes a string input and its length size as parameters.

Step 2: Split input into an array of words and Store the first word in a temporary variable temp.

Step 3: Swap the first and last words in the array and Reassign the last word to temp.

Step 4: Initialize an empty string reversedMiddle.

Step 5: For each word from the second to the second-last, reverse it and append it to reversedMiddle.

Step 6: Print the modified String: swapped first and last words with reversedMiddle in between.

Step 7: In the main method, define a sample input string exampleInput. Calculate the length of exampleInput and store it in inputSize.

Step 8: Call performSwapAndReverse with exampleInput and inputSize.

Implementation:

Filename: StringManipulation.java

Output:

Modified String: Java ni elpmaxe esrever dna Swap

Time Complexity: The time complexity of the above code is O(n). The code's time complexity is linear due to the need to split the String and reverse the middle characters, which depends on the input string's length.

Auxiliary Space: The code has the auxiliary space of O(n). The auxiliary space is linear, driven by the storage needed for the array of words from the split operation and the String for the reversed middle section.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA