编写示例以使用递归查找给定的字符串是否为回文
递归是以自相似的方式重复项目的过程。在编程语言中,如果程序允许您在同一函数内调用函数,则称为函数的递归调用。
以下是使用递归函数查找给定数量回文的示例。
示例
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");
}
}
}输出结果
Given String is a palindrome
热门推荐
4 入学校简短祝福语
10 哥哥新婚祝福语创意简短
11 兄弟的结婚祝福语简短
12 英文写结婚祝福语简短
13 恩师退休花束祝福语简短
14 财运健康成语祝福语简短
15 节前送温暖祝福语简短
16 忙碌生活祝福语简短英文
17 打游戏通关祝福语简短
18 送给员工美好祝福语简短