本文主要是介绍输入一个字符串,判断该字符串插入一个字符,能否使其成为回文字符串,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如果是输出YES,否输出NO,如果本来就是回文串,会输出YES
package com.zqd.file;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);String str = input.next();StringBuffer strBuffer = new StringBuffer(str);String str2 = strBuffer.reverse().toString();char[] ch2 = new char[str2.length()+1];for (int i = 0; i < str2.length(); i++) {ch2[i] = str2.charAt(i);}for (int i = 0; i < str.length(); i++) {if(str.charAt(i)==str2.charAt(i))continue;char cha = str.charAt(i);for (int j = str.length()-1; j >=i ; j--) {ch2[j+1] = ch2[j];}ch2[i] = cha;break;}StringBuffer str3Buffer = new StringBuffer();for (int i = 0; i < ch2.length; i++) {str3Buffer.append(ch2[i]);}StringBuffer str4Buffer = new StringBuffer(str3Buffer);String str3 = str3Buffer.toString().trim();String str4 = str4Buffer.reverse().toString().trim();System.out.println(str3);System.out.println(str4);if(str3.equals(str4))System.out.println("YES");else{System.out.println("NO");}}}
coco
cococ
cococ
YES
这篇关于输入一个字符串,判断该字符串插入一个字符,能否使其成为回文字符串的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!