Given string str, the task is to write a Java program to swap the first and the last character of the given string and print the modified string.
Examples:
Input: str = “GeeksForGeeks”
Output: seeksForGeekG
Explanation: The first character of the given string is ‘G’ and the last character of the given string is ‘s’. We swap the character ‘G and ‘s’ and print the modified string.Input: str = “Java”
Output: aavJ
Explanation: The first character of the given string is ‘J’ and the last character of the given string is ‘a’. We swap the character ‘J and ‘a’ and print the modified string.
Method 1 – using String.toCharArray() method
- Get the string to swap first and last character.
- Check if the string has only one character then return the string.
- Convert the given string into a character array.
- Swap first and the last character of the string using a temp variable.
- Now, print the modified string.
Below is the implementation of the above approach:
Java
// Java program for the above approach class GFG { // Function that swap first and // the last character of a string public static String swapFirstAndLast(String str) { // Check if the string has only // one character then return // the string if (str.length() < 2 ) return str; // Converting the string into // a character array char [] ch = str.toCharArray(); // Swapping first and the last // character of a string char temp = ch[ 0 ]; ch[ 0 ] = ch[ch.length - 1 ]; ch[ch.length - 1 ] = temp; // Converting character to // string and return return String.valueOf(ch); } // Driver Code public static void main(String args[]) { // Given String str String str = "GeeksForGeeks" ; // Function Call System.out.println( swapFirstAndLast(str)); } } |
seeksForGeekG
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 2 – using StringBuilder.setCharAt() method:
- Get the string to swap first and the last character.
- Check if the string has only one character then return the string.
- Create a StringBuilder object with the given string passed as a parameter.
- Set the last character of a string at index zero.
- Set the first character of a string at the last index.
- Now, print the modified string.
Below is the implementation of the above approach:
Java
// Java program for the above approach class GFG { // Function that swap first and // the last character of a string public static String swapFirstAndLast(String str) { // Check if the string has only // one character then return // the string if (str.length() < 2 ) return str; // Creating a StringBuilder object // with given string StringBuilder sb = new StringBuilder(str); // Finding the first character // of the string char first = sb.charAt( 0 ); // Set last character at index zero sb.setCharAt( 0 , sb.charAt(sb.length() - 1 )); // Set first character at last index sb.setCharAt(sb.length() - 1 , first); // Converting StringBuilder to // String and return return sb.toString(); } // Driver Code public static void main(String args[]) { // Given String str String str = "GeeksForGeeks" ; // Print the modified string System.out.println( swapFirstAndLast(str)); } } |
seeksForGeekG
Time Complexity: O(N)
Auxiliary Space: O(N) it is using extra space for StringBuilder sb
Method 3 – using String.substring() method
- Get the string to swap first and the last character.
- Check if the string has only one character then return the string.
- Extract the last character of the string.
- Extract the first character of the string.
- Concatenate the last character and first character between the middle characters.
- Now, print the modified string.
Below is the implementation of the above approach:
Java
// Java program for the above approach class GFG { // Function that swap the first and // the last character of a string public static String swapFirstAndLast(String str) { // Check if the string has only // one character then return // the string if (str.length() < 2 ) return str; // Concatenate last character // and first character between // middle characters of string return (str.substring(str.length() - 1 ) + str.substring( 1 , str.length() - 1 ) + str.substring( 0 , 1 )); } // Driver Code public static void main(String args[]) { // Given String str String str = "GeeksForGeeks" ; // Function Call System.out.println( swapFirstAndLast(str)); } } |
seeksForGeekG
Time Complexity: O(N)
Auxiliary Space: O(1)