forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check palindrome in a string in Java (jainaman224#2491)
updated
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//Java program to check if a string is a Palindrome | ||
|
||
import java.util.*; | ||
|
||
public class reverse_string { | ||
|
||
// Reverse a given string | ||
public static String palindrome(String string_entered) { | ||
String str_reversed = ""; | ||
int length_of_String = string_entered.length()-1; | ||
int len = 0; | ||
|
||
for (len = length_of_String; len >= 0 ; len --) | ||
str_reversed = str_reversed + string_entered.charAt(len); | ||
return str_reversed; | ||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
Scanner s = new Scanner(System.in); | ||
System.out.println(" Hey,Enter any string "); | ||
String string_entered = s.nextLine(); | ||
String str_reversed = palindrome(string_entered); | ||
|
||
// Comparing 2 strings to find if input string is palindrome or not | ||
if(string_entered.equals(str_reversed)) { | ||
System.out.println(" String is a PALINDROME "); | ||
} | ||
else { | ||
System.out.println(" String is not a PALINDROME "); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
Test cases | ||
INPUT | ||
Hey,Enter any string | ||
hello | ||
OUTPUT | ||
String is not a PALINDROME | ||
INPUT | ||
Hey,Enter any string | ||
metem | ||
OUTPUT | ||
String is a PALINDROME | ||
*/ | ||
|