📌  相关文章
📜  交换Java字符串的第一个和最后一个字符

📅  最后修改于: 2021-05-20 05:22:26             🧑  作者: Mango

在给定字符串str的情况下,任务是编写一个Java程序以交换给定字符串的第一个和最后一个字符,并打印修改后的字符串。

例子:

方法1 –使用String.toCharArray()方法

  1. 获取字符串以交换第一个和最后一个字符。
  2. 检查字符串是否只有一个字符,然后返回字符串。
  3. 将给定的字符串转换为字符数组。
  4. 使用临时变量交换字符串的第一个和最后一个字符。
  5. 现在,打印修改后的字符串。

下面是上述方法的实现:

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()方法

  1. 获取要交换的字符串的第一个和最后一个字符。
  2. 检查字符串是否只有一个字符,然后返回字符串。
  3. 创建一个StringBuilder对象,并将给定的字符串作为参数传递。
  4. 将字符串的最后一个字符设置为索引零。
  5. 将字符串的第一个字符设置在最后一个索引处。
  6. 现在,打印修改后的字符串。

下面是上述方法的实现:

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()方法

  1. 获取要交换的字符串的第一个和最后一个字符。
  2. 检查字符串是否只有一个字符,然后返回字符串。
  3. 提取字符串的最后一个字符。
  4. 提取字符串的第一个字符。
  5. 将最后一个字符和第一个字符连接在中间字符之间。
  6. 现在,打印修改后的字符串。

下面是上述方法的实现:

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)