博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java字符串最长回文串_Java中的字符串回文程序
阅读量:2531 次
发布时间:2019-05-11

本文共 3484 字,大约阅读时间需要 11 分钟。

java字符串最长回文串

Given a string and we have to check whether it is palindrome string or not.

给定一个字符串,我们必须检查它是否是回文字符串。

A string that is equal to its reverse string is known as palindrome string. To implement the program for checking whether a given string is a palindrome or not, we have created a function "isPalindrome()".

等于其反向字符串的字符串称为回文字符串 。 为了实现检查给定字符串是否为回文程序 ,我们创建了一个函数“ isPalindrome()”

In the function,

在功能上

  • We are checking for whether a string is an empty string or not – if the string is an empty string then throwing an error.

    我们正在检查一个字符串是否为空字符串-如果该字符串为空字符串,则抛出错误。

  • Then, we are converting string to uppercase to make comparison case insensitive.

    然后,我们将字符串转换为大写以使比较大小写不敏感。

  • Then, running a loop from 0 to len/2, to compare the first character with last character, the second character with second last character and so on..., and checks whether they are equal or not if both the elements are equal it goes for the next one. If not, then code returns false. Going on comparing first and last elements of the string if it reaches the length/2 mark then the loop ends, and return true for Palindrome.

    然后,从0到len / 2循环运行,比较第一个字符与最后一个字符,第二个字符与倒数第二个字符,依此类推...,并检查两个元素是否相等,是否相等?去下一个。 如果不是,则代码返回false。 继续比较字符串的第一个和最后一个元素(如果它达到length / 2标记),则循环结束,并为回文式返回true。

用于检查字符串回文的Java代码 (Java code for checking string palindrome)

// Java code for checking string palindrome public class Main {
//function to check whether string is Palindrome or not public static boolean isPalindrome(String str) {
// Checking for null if (str == null) {
throw new IllegalArgumentException("String is null."); } // length of the string // if there is one character string - returing true int len = str.length(); if (len <= 1) {
return true; } // Converting the string into uppercase // to make the comparisons case insensitive String strU = str.toUpperCase(); // result variable // default initializing it with true boolean result = true; for (int i = 0; i < len / 2; i++) {
if (strU.charAt(i) != strU.charAt(len - 1 - i)) {
result = false; // break the loop if the condition is true break; } } return result; } //main code public static void main(String[] args) {
String str1 = "Hello world!"; if (isPalindrome(str1)) {
System.out.println(str1 + " is a palindrome string "); } else {
System.out.println(str1 + " is not a palindrome string "); } String str2 = "ABCxCBA"; if (isPalindrome(str2)) {
System.out.println(str2 + " is a palindrome string "); } else {
System.out.println(str2 + " is not a palindrome string "); } String str3 = "noon"; if (isPalindrome(str3)) {
System.out.println(str3 + " is a palindrome string "); } else {
System.out.println(str3 + " is not a palindrome string "); } String str4 = "nooN"; if (isPalindrome(str4)) {
System.out.println(str4 + " is a palindrome string "); } else {
System.out.println(str4 + " is not a palindrome string "); } }}

Output

输出量

Hello world! is not a palindrome stringABCxCBA is a palindrome stringnoon is a palindrome stringnooN is a palindrome string

翻译自:

java字符串最长回文串

转载地址:http://ettzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_02 微服务核心基础讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-01 什么是微服务的注册中心
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-03CAP原理、常见面试题
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-06 服务注册和发现之Eureka Client搭建商品服务实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-07 Eureka服务注册中心配置控制台问题处理...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-01 常用的服务间调用方式讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-02 微服务调用方式之ribbon实战 订单调用商品服务...
查看>>