-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPalindrome string
More file actions
51 lines (40 loc) · 928 Bytes
/
Palindrome string
File metadata and controls
51 lines (40 loc) · 928 Bytes
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
44
45
46
47
48
49
50
51
// Java program to check whether a
// string is a Palindrome
// Using two pointing variables
// Main class
public class GFG {
// Method
// Returning true if string is palindrome
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
// Method 2
// main driver method
public static void main(String[] args)
{
// Input string
String str = "geeks";
// passing bool function till holding true
if (isPalindrome(str))
// It is a pallindrome
System.out.print("Yes");
else
// Not a pallindrome
System.out.print("No");
}
}