Created
July 10, 2020 07:17
-
-
Save JosephineAkello/7598f96a1d1592c0ebd438374afb1589 to your computer and use it in GitHub Desktop.
Reverse a string in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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