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(1)
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)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java program to swap first and last characters of words in a sentence
- How to find the first and last character of a string in Java
- Remove first and last character of a string in Java
- Print the first and last character of each word in a String
- Remove the first and last character of each word in a string
- Capitalize the first and last character of each word in a string
- Reverse every word of the string except the first and the last character
- Remove the first and last occurrence of a given Character from a String
- Minimum operation require to make first and last character same
- Queries to find the last non-repeating character in the sub-string of a given string
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Find the character in first string that is present at minimum index in second string
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Queries to find the first non-repeating character in the sub-string of a string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Find last index of a character in a string
- Print last character of each word in a string
- Find the last non repeating character in string
- Find the last remaining Character in the Binary String according to the given conditions
- Find the player who is the last to remove any character from the beginning of a Binary String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

