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
class GFG {
public static String
swapFirstAndLast(String str)
{
if (str.length() < 2 )
return str;
char [] ch = str.toCharArray();
char temp = ch[ 0 ];
ch[ 0 ] = ch[ch.length - 1 ];
ch[ch.length - 1 ] = temp;
return String.valueOf(ch);
}
public static void
main(String args[])
{
String str = "GeeksForGeeks" ;
System.out.println(
swapFirstAndLast(str));
}
}
|
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
class GFG {
public static String
swapFirstAndLast(String str)
{
if (str.length() < 2 )
return str;
StringBuilder sb
= new StringBuilder(str);
char first = sb.charAt( 0 );
sb.setCharAt( 0 ,
sb.charAt(sb.length() - 1 ));
sb.setCharAt(sb.length() - 1 ,
first);
return sb.toString();
}
public static void
main(String args[])
{
String str = "GeeksForGeeks" ;
System.out.println(
swapFirstAndLast(str));
}
}
|
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
class GFG {
public static String
swapFirstAndLast(String str)
{
if (str.length() < 2 )
return str;
return (str.substring(str.length() - 1 )
+ str.substring( 1 , str.length() - 1 )
+ str.substring( 0 , 1 ));
}
public static void
main(String args[])
{
String str = "GeeksForGeeks" ;
System.out.println(
swapFirstAndLast(str));
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)