Skip to content

Instantly share code, notes, and snippets.

@JosephineAkello
Created July 10, 2020 07:17
Show Gist options
  • Save JosephineAkello/7598f96a1d1592c0ebd438374afb1589 to your computer and use it in GitHub Desktop.
Save JosephineAkello/7598f96a1d1592c0ebd438374afb1589 to your computer and use it in GitHub Desktop.
Reverse a string in Java
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
scan.close();
/* Reverse string and compare to original */
/* If a String is equivalent to itself when reversed, it's a palindrome */
String reversed = new StringBuilder(str).reverse().toString();
System.out.println(str.equals(reversed) ? "Yes" : "No");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment