判断是否是回文字符串

最近写作业写到回文,用栈来判断,我是将字符串push后在pop,然后比较两个字符串是否相同,相同就是回文字符串,不同就不是。以下是我的代码,有些粗糙,希望能得到大家的指导。

package data;
/**
 * 将str压入栈中,然后将栈中数据出栈,
 * 比较s和str是否相同,
 * 相同即回文,不同即非回文
 */
import java.util.*;
public class Circle {
	public static void main(String[] args) throws Exception {
		System.out.println("输入一段英文字符串:");
		Scanner sc = new Scanner(System.in);
		if(isCircle(sc.next()))
			System.out.println("该字符串为回文字符");
		else
			System.out.println("该字符串不是回文字符");
	}
	public static boolean isCircle(String str) throws Exception {
		
		int length = str.length();
		Stack st = new Stack(length);
		for(int i=0;i<length;i++) {
			char c = str.charAt(i);
			String t = String.valueOf(c);
			st.push(t);
		}
		String s = "";
		for(int i=0;i<length;i++) {
			s=s+st.pop().toString();
			
		}
		if(s.equals(str))
			return true;
		else
			return false;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值