Answers for "how to traverse a palindrome java using substring method"

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

String palindrome in java using array

// String palindrome in java using array
import java.util.Arrays;
import java.util.Scanner;
public class StringPalindromeUsingArray 
{
   public static void main(String[] args) 
   {
      System.out.println("Please enter string to check whether string is palindrome: ");
      Scanner sc = new Scanner(System.in);
      String strInput = sc.nextLine();
      char[] chArray = strInput.toCharArray();
      int size = chArray.length;
      char[] chGiven = Arrays.copyOf(chArray, chArray.length);
      for(int a = 0; a < size / 2; a++) 
      {
         char temp = chArray[a];
         chArray[a] = chArray[size - a - 1];
         chArray[size - a - 1] = temp;
      }
      System.out.println("Given array: " + Arrays.toString(chGiven));
      System.out.println("String palindrome using array: " + Arrays.toString(chArray));
      if(Arrays.equals(chArray, chGiven)) 
      {
         System.out.println("string is palindrome.");
      }
      else 
      {
         System.out.println("string is not a palindrome.");
      }
      sc.close();
   }
}
Posted by: Guest on February-22-2021

Code answers related to "how to traverse a palindrome java using substring method"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language