编程知识 cdmana.com

Java and PHP detect whether the string is palindrome string

Java:

import java.util.Scanner;

public class CheckHuiWen {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine();
        System.out.println(" character string 【" + string + "】"+(check(string) ? " yes " : " No ")+" Palindrome string ");
    }

    private static boolean check(String string) {
        int length = string.length();
        int count = length / 2;
        for(int i = 0; i < count; i++) {
            if(string.charAt(i) != string.charAt(length - i - 1)) {
                return false;
            }
        }
        return true;
    }
}

PHP:

<?php
function check(string $string) {
    $length = strlen($string);
    $count = $length / 2;
    for($i = 0; $i < $count; $i++) {
        if($string[$i] != $string[$length - $i - 1]) {
            return false;
        }
    }
    return true;
}

$string = 'level';
echo " character string 【{$string}】".(check($string) ? ' yes ' : ' No ').' Palindrome string '.PHP_EOL;

 

版权声明
本文为[OSC chief security guard]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225092906494v.html

Scroll to Top