-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringPalindrome.java
51 lines (45 loc) · 1.24 KB
/
StringPalindrome.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
package com.javamultiplex.string;
import java.util.Scanner;
/**
*
* @author Rohit Agarwal
* @category String Problems
* @problem String is palindrome or not?
*
*/
public class StringPalindrome {
/*
* 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 1st method - using library function of String class
*/
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(System.in);
System.out.println("Enter String : ");
String string = input.next();
/*
* There are 3 ways to Reverse any String.
* 1. Using library function of StringBuilder class.
* 2. Using Iteration method.
* 3. Using Recursion method.
*
* Here we are using 1st method - using library function.
*/
StringBuilder stringBuilder = new StringBuilder(string);
String newString = stringBuilder.reverse().toString();
if (string.equals(newString)) {
System.out.println("String is Palindrome.");
} else {
System.out.println("String is not Palindrome.");
}
} finally {
if (input != null) {
input.close();
}
}
}
}