迴文字元java
1. java紼嬪簭銆傚瓧絎︿覆鍒ゆ柇鏄鍚︿負鈥滃洖鏂団
鏂規硶鏀逛負
static boolean isPalindrome(String str) {//鍒ゆ柇瀛楃︿覆鏄鍚︿負鈥滃洖鏂団
StringBuffer buf = new StringBuffer(str).reverse();
return buf.toString().equals(str);
}
2. 銆恓ava銆戠紪鍐欑▼搴忥紝瑕佹眰鍒ゆ柇浠庨敭鐩樿緭鍏ョ殑瀛楃︿覆鏄鍚︿負鍥炴枃
瀵逛簬鍥炴枃榪欎釜闂棰橈紝鎴戜滑涓鑸鐨勮В鍐蟲柟娉曟槸鐢ㄩ掑綊
package com.axjy.yzn;
public class Recursive {
public static void main(String[] args) {
System.out.println("Is abcba a Palindrome?"+isPalindrome("abcdcba"));
}
public static boolean isPalindrome(String s){
if(s.length()<=1){
return true;
}else if(s.charAt(0) != s.charAt(s.length()-1)){
return false;
}
return isPalindrome(s.substring(1,s.length()-1));
}
}
3. Java語言如何判斷一個字元串是否迴文
Java語言判斷一個字元串是否迴文可以通過下面的函數進行判斷:
import java.util.Scanner;
public class huiwen
{public static void main(String args[])
{Scanner sca=new Scanner(System.in);
String str=sca.nextLine();
int a=str.length();
int middle=a/2,i=3;
//char c1=(char) a,c2=(char) a;
for(i=0;i<middle&&str.charAt(i)==str.charAt(a-1-i);i++)
{}
if(i<middle)
System.out.println(str+"不是迴文");
else
System.out.println(str+"是迴文");}}
(3)迴文字元java擴展閱讀:
c/c++判斷一個字元串是否迴文的方法:
#include <iostream>
#include <string>
using namespace std;
int main()
{string str; int i,j,l;int flag = 1;
while (cin >> str)
{ l = str.length(); for (i = 0,j = l-1; i <= j; i++,j--)
{ if (str[i] != str[j])
{ flag = 0;break; }}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
flag = true;}
return 0;}