回文字符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;}