Created
March 30, 2019 16:05
-
-
Save isauravmanitripathi/559fec3d2c70e2891e6ed8b4e7830c26 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
/** | |
* Program to check for a palindrome. | |
* | |
* by saurav mani tripathi | |
* for medium.com/damnn | |
*/ | |
public class Palindrome { | |
private String reverse (final String s) { | |
String result = new String(); | |
int position = 0; | |
while (position < s.length()) { | |
result = (new Character(s.charAt(position))).toString() + result; | |
position = position + 1; | |
} | |
return result; | |
} | |
/** | |
* Check to see if the two argument <code>String</code>s are the reverse of each other. | |
* | |
* return true if they are and false otherwise. | |
*/ | |
private boolean check (final String s1, final String s2) { | |
final String s = reverse(s2); | |
if (s1.compareTo(s) == 0){return true;} | |
else {return false;} | |
} | |
private String getInput() { | |
Scanner in = new Scanner(System.in); | |
System.out.println("Enter text to check: "); | |
return in.nextLine(); | |
} | |
private void testForPalindrome(final String s) { | |
String testString = s.toLowerCase(); | |
if (check(testString, testString)) {System.out.println("The string is palindrome.");} | |
else {System.out.println("The string is not a palindrome.");} | |
} | |
public static void main(final String [] args) { | |
final Palindrome object = new Palindrome(); | |
object.testForPalindrome(object.getInput()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment