Skip to content

Commit

Permalink
Palindrome in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
stewtech committed Jun 3, 2022
1 parent 88142c0 commit 18b0a9b
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Palindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

// *******************************************************************
// Palindrome.java Reads in a string and prints a message saying whether it
// is a palindrome.
// *******************************************************************

String testString;
Scanner scan = new Scanner(System.in);

System.out.print("Enter a string: ");
testString = scan.nextLine();

if (palindrome(testString)) {
System.out.println("It's a palindrome!");
} else {
System.out.println("It's not a palindrome.");
}
}

// ----------------------------------------------------------
// Recursively determines whether s is a palindrome.
// It is if
// -- it's 0 or 1 char in length, or
// -- the first and last letters are the same and the
// string without those letters is also a palindrome
// ----------------------------------------------------------

private static boolean palindrome(String s) {
if (s.length() == 0 || s.length() == 1) {
return true;
}
if (s.charAt(0) != s.charAt(s.length() - 1)) {
return false;
}

return palindrome(s.substring(1, s.length() - 1));
}

// CREATE A INTEGER length THAT STORES THE LENGTH OF THE STRING PARAMETER s

// 1) TEST IF length IS GREATER THAN 1 -
// 2) IF length IS GREATER THAN 1, CHECK IF THE FIRST CHARACTER OF s IS EQUAL TO
// THE CHARACTER AT length - 1
// 3) IF THE CHARACTERS ARE EQUAL, TEST THE NEXT SET OF CHARACTERS (MAKE A
// RECURSIVE
// CALL AND TEST THE VALUE THAT IS RETURNED)
// 4) IF NONE OF THESE TEST CASES ARE true, RETURN false
// (HINT: YOU SHOULD RETURN false 3 TIMES)

}

1 comment on commit 18b0a9b

@tangorishi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey I am looking for my first contribution , I think I got a different approach to solve this problem please assign this problem to me.

Please sign in to comment.