Given string str, the task is to write down a Java program to swap the primary and therefore 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
Below is the implementation of the above approach:
// 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)); } }
Output:
seeksForGeekG
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 2 – using StringBuilder.setCharAt() method:
Below is the implementation of the above approach:
// 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)); } }
Output:
seeksForGeekG
Method 3 – using String.substring() method
Below is the implementation of the above approach:
// 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)); } }
Output:
seeksForGeekG