日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java字符串最长回文串_Java中的字符串回文程序

發布時間:2025/3/11 java 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java字符串最长回文串_Java中的字符串回文程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 notpublic static boolean isPalindrome(String str) {// Checking for nullif (str == null) {throw new IllegalArgumentException("String is null.");}// length of the string// if there is one character string - returing trueint 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 trueboolean 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 truebreak;}}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 string ABCxCBA is a palindrome string noon is a palindrome string nooN is a palindrome string

翻譯自: https://www.includehelp.com/java-programs/string-palindrome-program-in-java.aspx

java字符串最長回文串

總結

以上是生活随笔為你收集整理的java字符串最长回文串_Java中的字符串回文程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。