📜  在Java中交换字符串中的字符对

📅  最后修改于: 2022-05-13 01:54:39.108000             🧑  作者: Mango

在Java中交换字符串中的字符对

给定字符串str ,任务是编写一个Java程序来交换字符串的字符对。如果字符串包含奇数个字符,则最后一个字符保持原样。

例子:

1. 使用String.toCharArray()方法

  1. 获取字符串交换一对字符。
  2. 检查字符串是否为空或空,然后返回字符串。
  3. 将给定的字符串转换为字符数组。
  4. 遍历字符数组并交换字符。
  5. 现在,打印结果。
Java
// Java program to swap pair
// of characters of a string
  
class GFG {
  
    // Function to swap pair of
    // characters of a string
    public static String swapPair(String str)
    {
  
        // Checking if string is null
        // or empty then return str
        if (str == null || str.isEmpty())
            return str;
  
        // Converting the given string
        // into a character array
        char[] ch = str.toCharArray();
  
        // Traverse the character array
        for (int i = 0; i < ch.length - 1; i += 2) {
  
            // Swapping the characters
            char temp = ch[i];
            ch[i] = ch[i + 1];
            ch[i + 1] = temp;
        }
  
        // Converting the result into a
        // string and return
        return new String(ch);
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given String str
        String str = "GeeksForGeeks";
  
        // Print the result
        System.out.println(swapPair(str));
    }
}


Java
// Java program to swap pair
// of characters of a string
  
class GFG {
  
    // Function to swap pair of
    // characters of a string
    public static String swapPairs(String str)
    {
  
        // Checking if string is null
        // or empty then return str
        if (str == null || str.isEmpty())
            return str;
  
        int len = str.length();
  
        // Creating a StringBuffer object with
        // length of the string passed as a parameter
        StringBuffer sb = new StringBuffer(len);
  
        // Traverse the string and append
        // the character in the StringBuffer
        // object in reverse order
        for (int i = 0; i < len - 1; i += 2) {
            sb.append(str.charAt(i + 1));
            sb.append(str.charAt(i));
        }
  
        // Checking if the string has
        // odd number of characters
        // then append the last character
        // into StringBuffer object
        if (len % 2 != 0) {
            sb.append(str.charAt(len - 1));
        }
  
        // Converting the StringBuffer
        // into the string and return
        return sb.toString();
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given String str
        String str = "GeeksForGeeks";
  
        // Print the result
        System.out.println(swapPairs(str));
    }
}


输出
eGkeFsroeGkes


2. 使用StringBuffer.append()方法

  1. 获取字符串交换一对字符。
  2. 检查字符串是否为空或空,然后返回字符串。
  3. 使用作为参数传递的字符串长度创建一个StringBuffer对象。
  4. 遍历字符串并以相反的顺序将字符追加到StringBuffer对象中。
  5. 检查字符串是否包含奇数个字符,然后将最后一个字符附加到StringBuffer对象中。
  6. 现在,打印结果。

Java

// Java program to swap pair
// of characters of a string
  
class GFG {
  
    // Function to swap pair of
    // characters of a string
    public static String swapPairs(String str)
    {
  
        // Checking if string is null
        // or empty then return str
        if (str == null || str.isEmpty())
            return str;
  
        int len = str.length();
  
        // Creating a StringBuffer object with
        // length of the string passed as a parameter
        StringBuffer sb = new StringBuffer(len);
  
        // Traverse the string and append
        // the character in the StringBuffer
        // object in reverse order
        for (int i = 0; i < len - 1; i += 2) {
            sb.append(str.charAt(i + 1));
            sb.append(str.charAt(i));
        }
  
        // Checking if the string has
        // odd number of characters
        // then append the last character
        // into StringBuffer object
        if (len % 2 != 0) {
            sb.append(str.charAt(len - 1));
        }
  
        // Converting the StringBuffer
        // into the string and return
        return sb.toString();
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given String str
        String str = "GeeksForGeeks";
  
        // Print the result
        System.out.println(swapPairs(str));
    }
}
输出
eGkeFsroeGkes