Open In App

Swap the first and last character of a string in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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

  1. Get the string to swap first and last character.
  2. Check if the string has only one character then return the string.
  3. Convert the given string into a character array.
  4. Swap first and the last character of the string using a temp variable.
  5. 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));
    }
}


Output

seeksForGeekG

Time Complexity: O(N)
Auxiliary Space: O(N)

Method 2 – using StringBuilder.setCharAt() method:

  1. Get the string to swap first and the last character.
  2. Check if the string has only one character then return the string.
  3. Create a StringBuilder object with the given string passed as a parameter.
  4. Set the last character of a string at index zero.
  5. Set the first character of a string at the last index.
  6. 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));
    }
}


Output

seeksForGeekG

Time Complexity: O(N)
Auxiliary Space: O(N) it is using extra space for StringBuilder sb

Method 3 – using String.substring() method

  1. Get the string to swap first and the last character.
  2. Check if the string has only one character then return the string.
  3. Extract the last character of the string.
  4. Extract the first character of the string.
  5. Concatenate the last character and first character between the middle characters.
  6. 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));
    }
}


Output

seeksForGeekG

Time Complexity: O(N)
Auxiliary Space: O(1)



Last Updated : 02 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads