forked from ShivangiSingh17/Java-Jet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanindrome.java
More file actions
43 lines (31 loc) · 1.03 KB
/
Panindrome.java
File metadata and controls
43 lines (31 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.jagjit.Hactoberfest;
import java.util.Scanner;
public class Panindrome {
// function to check palindrome
public static boolean isPalindrome(String s) {
// if length of the string 0 or 1 then String is palindrome
if (s.length() == 0 || s.length() == 1)
return true;
// check first and last char of String:
if (s.charAt(0) == s.charAt(s.length() - 1))
return isPalindrome(s.substring(1, s.length() - 1)); // Function calling itself: Recursion
// if control reaches where character does not match the return false
return false;
}
public static void main(String[] args) {
// For user input
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the String:");
String string = scanner.nextLine();
/*
* isPalindrome is a function which returns true if function is palindrome or
* not
*
*/
if (isPalindrome(string.toLowerCase()))
System.out.println(string + " is a palindrome");
else
System.out.println(string + " is not a palindrome");
}
}