Given a string, the task is to swap every two adjacent characters. If the string contains an odd number of characters, the last character remains unchanged.
- The task is to swap every pair of adjacent characters in a string.
- If the string has an odd length, the last character remains unchanged.
Examples:
Input: str = “Java”
Output: aJavInput: str = “GeeksForGeeks”
Output: eGkeFsroeGkes
Approches to Swap Pairs of Char acters in a String
The problem can be solved using the following approaches:
1. Using String.toCharArray()
- Convert the string into a character array.
- Traverse the array in steps of 2.
- Swap the current character with the next character.
- If the length is odd, the last character is automatically left unchanged.
- Convert the character array back to a string.
public class GFG {
// Method to swap adjacent character pairs
static String swapPairs(String str) {
// Handle null or empty string
if (str == null || str.isEmpty()) {
return str;
}
// Convert String to character array
char[] chars = str.toCharArray();
// Swap characters in pairs
for (int i = 0; i + 1 < chars.length; i += 2) {
char temp = chars[i];
chars[i] = chars[i + 1];
chars[i + 1] = temp;
}
// Convert character array back to String
return new String(chars);
}
public static void main(String[] args) {
String str = "GeeksForGeeks";
System.out.println("Original String: " + str);
System.out.println("Modified String: " + swapPairs(str));
}
}
Output
Original String: GeeksForGeeks Modified String: eGkeFsroeGkes
Explanation:
- toCharArray() converts the string into a mutable character array.
- i += 2 moves to the next pair of characters.
- chars[i] and chars[i + 1] are exchanged using a temporary variable.
- The condition i + 1 < chars.length prevents accessing an index outside the array.
- For an odd-length string, the last character is not swapped.
- new String(chars) creates the final string.
2. Using StringBuffer.append() Method
- Traverse the string in pairs.
- Append the second character followed by the first character.
- If the string has an odd length, append the last character separately.
- Convert the StringBuffer into a String.
public class GFG {
// Method to swap adjacent character pairs
static String swapPairs(String str) {
// Handle null or empty string
if (str == null || str.isEmpty()) {
return str;
}
StringBuffer result = new StringBuffer(str.length());
// Process characters in pairs
for (int i = 0; i + 1 < str.length(); i += 2) {
result.append(str.charAt(i + 1));
result.append(str.charAt(i));
}
// Append the last character if length is odd
if (str.length() % 2 != 0) {
result.append(str.charAt(str.length() - 1));
}
return result.toString();
}
public static void main(String[] args) {
String str = "GeeksForGeeks";
System.out.println("Original String: " + str);
System.out.println("Modified String: " + swapPairs(str));
}
}
Output
Original String: GeeksForGeeks Modified String: eGkeFsroeGkes
Explanation:
- Create a StringBuffer with the required capacity.
- Read the string two characters at a time.
- Append the second character first and then the first character.
- If the string length is odd, append the remaining last character.
- toString() converts the StringBuffer into the resulting string.