在给定字符串str的情况下,任务是编写一个Java程序以交换给定字符串的第一个和最后一个字符,并打印修改后的字符串。
例子:
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.
方法1 –使用String.toCharArray()方法
- 获取字符串以交换第一个和最后一个字符。
- 检查字符串是否只有一个字符,然后返回字符串。
- 将给定的字符串转换为字符数组。
- 使用临时变量交换字符串的第一个和最后一个字符。
- 现在,打印修改后的字符串。
下面是上述方法的实现:
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));
}
}
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));
}
}
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
时间复杂度: O(N)
辅助空间: O(N)
方法2 –使用StringBuilder.setCharAt()方法:
- 获取要交换的字符串的第一个和最后一个字符。
- 检查字符串是否只有一个字符,然后返回字符串。
- 创建一个StringBuilder对象,并将给定的字符串作为参数传递。
- 将字符串的最后一个字符设置为索引零。
- 将字符串的第一个字符设置在最后一个索引处。
- 现在,打印修改后的字符串。
下面是上述方法的实现:
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
时间复杂度: O(N)
辅助空间: O(1)
方法3 –使用String.substring()方法
- 获取要交换的字符串的第一个和最后一个字符。
- 检查字符串是否只有一个字符,然后返回字符串。
- 提取字符串的最后一个字符。
- 提取字符串的第一个字符。
- 将最后一个字符和第一个字符连接在中间字符之间。
- 现在,打印修改后的字符串。
下面是上述方法的实现:
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
时间复杂度: O(N)
辅助空间: O(1)