Skip to content

Commit 4753abc

Browse files
author
Kaveesha Baddage
committed
Add Palindrome function in Java
1 parent 56431b0 commit 4753abc

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

java/Palindrome.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Scanner;
2+
public class Palindrome {
3+
static boolean isPalindrome(String word) {
4+
char[] letterArray = word.toCharArray();
5+
int startingIndex = 0;
6+
int lastIndex = letterArray.length - 1;
7+
while (lastIndex > startingIndex) {
8+
if (letterArray[startingIndex] != letterArray[lastIndex]) {
9+
return false;
10+
}
11+
++startingIndex;
12+
--lastIndex;
13+
}
14+
return true;
15+
}
16+
public static void main(String args[]) {
17+
Palindrome palindrome = new Palindrome();
18+
while (true) {
19+
System.out.printf("Enter a word which needs to check whether a palindrome or not: ");
20+
Scanner scan = new Scanner(System.in);
21+
String input = scan.next();
22+
System.out.printf("The word '%s' is a palindrome : %s %n", input, palindrome.isPalindrome(input));
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)