Sunday, March 27, 2022

Check any string for palindrome using recursive function

 import java.io.*;
public class PalinRecursive
 {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
    void accept() throws Exception
    {
          System.out.print("\nEnter Sentence: ");
           str=br.readLine();
           int x=palin(str,0,str.length()-1);
           if(x==0)
           System.out.print("\nThe string "+str +" Palindrome");
           else
           System.out.print("\nThe string "+str +" is not Palindrome");
        }
           private int palin(String str,int start, int end)
       {
            if(start>=end)
            return 0;
            if(str.charAt(start)==str.charAt(end))
            return palin(str,++start,--end);
            else
            return 1;
        }
            
public static void main(String args[])throws Exception
{
    
PalinRecursive ob=new PalinRecursive();
    ob.accept();
}
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner