Skip to content

Commit 8dd6724

Browse files
authored
Merge pull request ephremdeme#81 from Halix267/all-contributors/add-palindrome
Create palindrome_detection.java
2 parents 06fb7aa + 888afc0 commit 8dd6724

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

palindrome_detection.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
4+
/**
5+
* Palindrome
6+
*/
7+
public class Palindrome {
8+
static boolean isPalindrome(String s) {
9+
10+
// Left and right pointers pointing to the beginning and the end of the string
11+
12+
int left = 0, right = s.length() - 1;
13+
14+
while (left < right) {
15+
16+
// If there is a mismatch
17+
18+
if (s.charAt(left) != s.charAt(right))
19+
return false;
20+
21+
// Increment left pointer and decrement right pointer
22+
left++;
23+
right--;
24+
}
25+
return true;
26+
}
27+
28+
public static void main(String[] args) {
29+
30+
Scanner sc = new Scanner(System.in);
31+
32+
String str = sc.nextLine(); //Input a string
33+
34+
System.out.println(isPalindrome(str)); // Print whether the input string is palindrome or not
35+
36+
}
37+
}

0 commit comments

Comments
 (0)