Palindrome string in Java

Check given string is palindrome or not


Palindrome string, a string that is same itself after reverse the string.
e.g: mom, dad, madam etc..
mom is a string after reverse it is a mom then that string is a palindrome.
mom=mom (after reverse) and madam=madam also
lets we check which type of strings are not a palindrome
hello is a string after reverse it olleh
hello=olleh (after reverse), so it is not palindrome

lets we check it programmatically



public class PalindromeString {
       public static void main(String[] args) {       
              String name="madam";
              String rev="";                          
              for(int i= name.length()-1;i>=0;i--){
                     rev=rev+name.charAt(i);                 
              }
              if(rev.equals(name)){
                     System.out.println(name+" is Palindrome string");                   
              }
              else{
                     System.out.println(name+" is not Palindrome string");
              }
       }
}


Output:

madam is Palindrome string

Understanding the flow of the program


String name="madam"; //check string is Palindrome or not
String rev="";      // rev is for store result,

name.length() for calculating string length here string madam length is 5 and index position is 0  to 4th

m
a
d
a
m
0
1
2
3
4

In for loop I take i=name.length()-1 loop means i=5-1=4 and here loop reapeat from 4th to 0 position lets check it in below chart.

for(int i=name.length()-1; i>=0; i--)
 {
rev=rev+name.charAt(i);
}
i=4; 4>=0; i- -    true
= “ ”+name.charAt(4)
rev=“ ”+m
i=3; 3>=0; i- -    true
= m+name.charAt(3)
rev=m+a
i=2; 2>=0; i- -    true
= ma+name.charAt(2)
rev=ma+d
i=1; 1>=0; i- -    true
= mad+name.charAt(1)
rev=mad+a
i=0; 0>=0; i- -    true
= mada+name.charAt(0)
rev=mada+m => madam

I hope you guys understood the flow of the program well J  and if any doubts please comment below.

Program for check given string palindrome or not



import java.util.Scanner;
public class PalindromeString {
       public static void main(String[] args) {       
              Scanner sc=new Scanner(System.in);
              System.out.println("Enter string");
              String name=sc.nextLine();
              String rev="";                          
              for(int i=name.length()-1;i>=0;i--){
                     rev=rev+name.charAt(i);                 
              }
              if(rev.equals(name)){
                     System.out.println(name+" is Palindrome string");                   
              }
              else{
                     System.out.println(name+" is not Palindrome string");
              }
       }
}


Output:

Enter string
mom
mom is Palindrome string
In the above program, we entered string through the keyboard.

Reverse String using StringBuffer


We can reverse a string by using StringBuffer class, StringBuffer class has reverse() method to reverse a string.

public class PalindromeString {
       public static void main(String[] args) {       
              String str="dad";
              StringBuffer br=new StringBuffer(str);
              br.reverse();
              if(br.equals(str)){
                     System.out.println(str+" is Palindrome string");                    
              }
              else{
                     System.out.println(str+" is Palindrome string");
              }
       }
}


Output:

dad is Palindrome string



No comments:

Post a Comment