课后作业:使用递归的方式判断一个字符串是否为回文


程序设计思想:

输入一个字符串,然后将字符串倒置,比较字符串第i位上的字符与倒数第i位上的字符是否相同,如果都相同则字符串是回文;否则字符串不是回文。

程序流程图:

 

源程序:

package huiwen;//信 1605-3 张运涛20163432

import java.util.Scanner;

public class Hunwen {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入一个字符串:");
Scanner input=new Scanner(System.in);

String str="";
str=input.next();
StringBuffer sb=new StringBuffer(str);
sb.reverse();//字符串倒置
int count=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==sb.charAt(i))//判断字符串的第i位上字符与倒数第i位上字符是否相同
{
count++;
}
}
if(count==str.length())
{
System.out.println(str+"是回文字符串");
}else
{
System.out.println(str+"不是回文字符串");
}
}

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM