Write a java program to Swap Characters.

Write a program to input a String and swap every 2 characters in the string. If the size is an odd number then keep the last letter as it is. Print the final swapped string. The return type (String) should return the character swapped string.

Input and Output Format

  • Input consists of a string.
  • The output consists of a string.

Refer sample output for formatting specifications

Sample Input 1:

TRAINER

Sample Output 1:

RTIAENR

Sample Input 2:

TOM ANDJERRY

Sample output 2:

OT MNAJDREYR

Program to Swap Characters in Java

Following are the steps to swap characters:

  • Input string from the user.
  • Pass the string to getvalues() method.
  • Inside the method, create an empty string buffer.
  • Now, get the length of the string into a variable l.
  • Now, check if the length is even and perform the following operations:
    • Iterate over the string using for loop and get the character value from the position i and i+1 in b and a. Increment the index by 2.
    • Next, append the value in ‘b’ to ‘a’ in a string buffer.
  • If the length is odd, then perform the following operation:
    • Iterate over the string using for loop and get the character value from the position i and i+1 in b and a. Increment the index by 2.
    • Next, append the value in ‘b’ to ‘a’ in a string buffer.
  • Now, get the character from the last position and append it to the string buffer.
  • At last, print the value in a string buffer.
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);
      }
      sb.append(s1.charAt(l - 1));
      System.out.println(sb);
    }
  }
}

Output

Swap First and Second Characters in Java

Input and Output Format

  • Input consists of a string.
  • The output consists of a string.

Refer sample output for formatting specifications

Sample Input 1:

Hello

Sample Output 1:

eHllo

Java Program to swap first and second characters

Following are the steps to swap first and second characters in Java:

  • Input string from the user.
  • Pass the string to getvalues() method.
  • Inside the method, convert the string into a character array.
  • Now, swap the first and second characters and print them.
package com.demo3;

import java.util.Scanner;

public class SwapCharacter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        getvalues(s1);
    }

    public static void getvalues(String s1) {

        char c [] = s1.toCharArray();
        char temp = c[0];
        c[0] = c[1];
        c[1] = temp;
        System.out.println(c);
    }
}

Output

Thus, in this way, we learned how to swap characters in a string.