-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringPalindromeUsingIteration.java
52 lines (46 loc) · 1.1 KB
/
StringPalindromeUsingIteration.java
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
52
package com.javamultiplex.string;
import java.util.Scanner;
/**
*
* @author Rohit Agarwal
* @category String Problems
* @problem String is palindrome or not?
*
*/
public class StringPalindromeUsingIteration {
/**
* There are 2 ways to check whether String is Palindrome or not.
* 1.Using library function of String class.
* 2.Using Iteration.
*
* Here we are using 2nd method - using Iteration.
*/
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(System.in);
System.out.println("Enter String : ");
String string = input.next();
int length = string.length();
int j = length - 1;
int count = 0, halflength = length / 2;
for (int i = 0; i < halflength; i++) {
if (string.charAt(i) == string.charAt(j)) {
count++;
} else {
break;
}
j--;
}
if (count == halflength) {
System.out.println("String is Palindrome.");
} else {
System.out.println("String is not Palindrome.");
}
} finally {
if (input != null) {
input.close();
}
}
}
}