Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Write an example to find whether a given string is palindrome using recursion
Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
Following is an example to find palindrome of a given number using recursive function.
Example
public class PalindromeRecursion {
public static boolean isPalindrome(String str){
if(str.length() == 0 ||str.length()==1){
return true;
}
if(str.charAt(0) == str.charAt(str.length()-1)){
return isPalindrome(str.substring(1, str.length()-1));
}
return false;
}
public static void main(String args[]){
String myString = "malayalam";
if (isPalindrome(myString)){
System.out.println("Given String is a palindrome");
}else{
System.out.println("Given String is not a palindrome");
}
}
}
Output
Given String is a palindrome
Advertisements
