'''
import java.util.Scanner;
public class recursionExercision {
Код: Выделить всё
//the recursive function that checks to see whether the
//string is a palindrone or not
public static boolean checkPalindrome(String str, int firstChar, int lastChar) {
//if only one character exists
if (firstChar == lastChar)
return true;
//checks to see if the first and last characters match
if ((str.charAt(firstChar)) != (str.charAt(lastChar)))
return false;
//checks to see if it has characters
if (!isChar(str))
return true;
//checks the middle strings with multiple characters
//on whether or not they are a palindrome with recursive method
if (firstChar < lastChar + 1)
return checkPalindrome(str, firstChar + 1, lastChar - 1 );
return true;
}
//method that actually determines what a palindrome is
public static boolean isAPalindrome(String str) {
int n = str.length();
//if string is not 0 or 1 then it's a palindrome
if(n == 0 || n == 1)
return false;
return checkPalindrome(str, 0, n - 1);
}
//method that checks for characters
public static boolean isChar(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!Character.isLetter(c) && !Character.isDigit(c))
return false;
}
return true;
}
//tests out recursive methods
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string to see if it's a palindrome:");
String str = scanner.nextLine(); //input from the user
//checks to see if it's a palindrome and puts them all
//to be lower case to ignore the case issue
if(isAPalindrome(str.toLowerCase()))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");
scanner.close();
}
Подробнее здесь: https://stackoverflow.com/questions/721 ... ers-in-all
Мобильная версия