Write a Program that accepts a string as a parameter and returns the string with each pair of adjacent letters reversed. If the string has an odd number of letters, the last letter is unchanged.
Input and Output Format:
Input consists of a string with maximum size of 100 characters.
Output consists of a single string.
Refer sample output for formatting specifications.
Sample Input 1:
forget
Sample Output 1:
ofgrte
Sample Input 2:
New York
Sample Output 2:
eN woYkr
Find Adjacent Swaps in Java.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
getvalues(s1);
}
public static void getvalues(String s1) {
StringBuffer sb = new StringBuffer();
int l = s1.length();
if (l % 2 == 0) {
for (int i = 0; i < s1.length() - 1; i = i + 2) {
char a = s1.charAt(i);
char b = s1.charAt(i + 1);
sb.append(b).append(a);
}
System.out.println(sb);
} else {
for (int i = 0; i < s1.length() - 1; i = i + 2) {
char a = s1.charAt(i);
char b = s1.charAt(i + 1);
sb.append(b).append(a);
System.out.println(sb);
}
sb.append(s1.charAt(l - 1));
System.out.println(sb);
}
}
}