Answers for "palindrome string program in java using recursion"

7

Java program to check palindrome string using recursion

import java.util.Scanner;
public class RecursivePalindromeJava 
{
   // to check if string is palindrome using recursion
   public static boolean checkPalindrome(String str)
   {
      if(str.length() == 0 || str.length() == 1)
         return true; 
      if(str.charAt(0) == str.charAt(str.length() - 1))
         return checkPalindrome(str.substring(1, str.length() - 1));
      return false;
   }
   public static void main(String[]args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a string : ");
      String strInput = sc.nextLine();
      if(checkPalindrome(strInput))
      {
         System.out.println(strInput + " is palindrome");
      }
      else
      {
         System.out.println(strInput + " not a palindrome");
      }
      sc.close();
   }
}
Posted by: Guest on November-02-2020
1

Java program to check palindrome number using recursion

// Java program to check palindrome number using recursion
public class PalindromeNumberRecursion
{
   static int palindromeRecursion(int number, int temp)
   {
      if(number == 0)
         return temp;
      // storing reverse of a number
      temp = (temp * 10) + (number % 10);
      return palindromeRecursion(number / 10, temp);
   }
   public static void main(String[] args)
   {
      int number = 141;
      int temp = palindromeRecursion(number, 0);
      if(temp == number)
      {
         System.out.println("number is palindrome.");
      }
      else
      {
         System.out.println("number is not palindrome.");
      }
   }
}
Posted by: Guest on February-19-2021
0

Palindrome string program in java using scanner

class Main
{
public static void main(String args[])
{
String a, b = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter the string");
a = in.nextLine();
int length = a.length();
for (int i = length - 1; i >= 0; i--)
b = b + a.charAt(i);
if(a.equals(b))
System.out.println("The string is a palindrome.");
else
System.out.println("The string is not a palindrome.");
}
}
Posted by: Guest on June-12-2021

Code answers related to "palindrome string program in java using recursion"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language