我想在不使用内置函数的情况下为String回文制作程序。
以下是我到目前为止所尝试的代码:
public class Palindrom
{
private static Scanner in;
public static void main(String[] args)
{
String s,str1,str2;
Scanner scan =new Scanner (System.in);
System.out.println("Enter the string");
String s = in.nextLine();
StringBuffer str1 = new StringBuffer();
StringBuffer str2 = new StringBuffer();
str1.reverse();
System.out.println("orignal string="+str2);
System.out.println("reveser string="+str1);
if(String.valueOf(str1).compareTo(String.valueOf(str2))==0)
System.out.println("palindrom");
else
System.out.println("not palindrom");
}
}
这个程序运行不正常。我认为问题在in.nextLine
和字符串缓冲区。
答案 0 :(得分:0)
下面给出的是检查此处输入的代码是否为回文的最简单方法。
import java.util.Scanner;
public class Strng {
public static void main(String[] args) {
String r = ""; //To store the reverse
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String s = sc.next(); // Entering the string
for(int i= s.length() - 1;i>=0;i--) {
r = r + s.charAt(i);
}
if(r.equals(s)) {
System.out.println("Is a palindrome");
}
else {
System.out.println("Not a palindrome");
}
}
}
答案 1 :(得分:0)
public static void palindrome(String str){
Map<Character,Integer> myMap = new HashMap<Character, Integer>();
int characterCounter = 1;
int index=-1;
for(char ch : str.toCharArray()){
index++;
if(str.length()%2!=0 && index==Math.abs(str.length()/2)) continue;
if(myMap.containsKey(ch))
characterCounter = myMap.get(ch)+1;
myMap.put(ch, characterCounter);
}
boolean flag = true;
for(Character myMApVal: myMap.keySet()){
if(myMap.get(myMApVal)%2!=0){
flag=false;
break;
}
}
if(!flag) System.out.println(str+" is not palindrome");
else System.out.println(str+" is palindrome");
}
答案 2 :(得分:0)
package practice;
import java.util.Scanner;
public class String_Palindrome {
public static void main(String[] args) {
//String s, str1, str2;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the string:");
String s = scanner.nextLine();
StringBuffer str1 = new StringBuffer(s);
StringBuffer str2 = new StringBuffer(s);
str1.reverse();
//System.out.println("Original String is:" + str2);
//System.out.println("Reverse String is:" + str1);
if (String.valueOf(str1).compareTo(String.valueOf(str2)) == 0)
System.out.println("Given String is a palindrome");
else
System.out.println("Given String is Not a palindrome");
}
}
答案 3 :(得分:-1)
import java.util.Scanner;
public class Palindrom {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter the string");
String inputStr =scanner.next();
System.out.println("Given String = "+inputStr);
char [] charArray=inputStr.toCharArray();
int strlength=(charArray.length)-1;
boolean isPalindrom=true;
for(int count=0; count<charArray.length && strlength >= 0; count++,strlength--){
if(charArray[count]!=charArray[strlength]){
isPalindrom=false;
break;
}
}
if(isPalindrom){
System.out.println("palindrom");
}else{
System.out.println("not palindrom");
}
}
}
解释:
取两点
字符串a [0]
的第一个开头字符串a [length-1]
比较char数组中的每个char
比较开始到结束并结束循环开始。
Palindrom案例1 :(长度为奇数)
“女士”是Palindrom:
a [0] == a [4] m
a [1] == a [3] a
a [2] == a [2] d
a [3] == a [1] a
a [4] == a [0] m
Palindrom案2 :(长度均匀)
“malayalam”是Palindrom:
a [0] == a [8] m
a [1] == a [7] a
a [2] == a [6] l
a [3] == a [5] a
a [4] == a [4] y
a [5] == a [3] a
a [6] == a [2] l
a [7] == a [1] a
a [8] == a [0] m
Not Palindrom案例3: 在每个字符中都不匹配(从开始到结束开始)。
答案 4 :(得分:-1)
import java.util.Scanner;
public class StringPalindrome {
public static void main(String[] args) {
Scanner sc = null;
String str1 = null;
// create object for scanner class
sc = new Scanner(System.in);
if (sc != null) {
System.out.println("Enter First String");
str1 = sc.nextLine();
}
strPalindrom(str1);
}// main
static void strPalindrom(String str) {
// converting string into array
char ch[] = str.toCharArray();
// check string is Palindrom or not
int count = ch.length - 1;
for (int i = 0; i < ch.length; i++, count--) {
if (ch[i] != ch[count]) {
System.out.println("String is not Palindrom");
break;
} else {
if (i == count) {
System.out.println("String is Palindrom");
}
}
} // for
}// strPalindrom method
}// class
答案 5 :(得分:-1)
我在上面看到了所有程序,但对初学者来说有点难以理解。
非常简单的初学者课程(检查String Palindrome)
public class Palindrome{
public static void main(String[] args) {
String s = "malayalam"; // enter here your requred String to check Palindrom
String rev = "";
for (int i = s.length() - 1; i >= 0; i--) {
rev = rev + s.charAt(i);
}
if (rev.equals(s)) {
System.out.println("Palindrom");
} else {
System.out.println("Not Palindrom");
}
}
}
答案 6 :(得分:-1)
import java.util.*;
class Test{
public static void main(String... as){
int check=0;
Scanner s= new Scanner(System.in);
String str = s.nextLine();
int l=str.length();
for (int i=0;i<l;i++){
if(str.charAt(i)!=str.charAt(l-1-i))
check++;
}
if(check==0)
System.out.println("yes it's palindrome");
else
System.out.println("no it's not.");
}
}
答案 7 :(得分:-3)
public class Palindrom {
public static void main(String[] args) {
String str= "dad";
char[] rev=str.toCharArray();
int i=0;
int j= rev.length-1;
while(i<j){
char temp =rev[i];
rev[i] = rev[j];
rev[j] = temp;
i++;
j--;
}
System.out.println(rev);
System.out.println();
if(str.equals(String.valueOf(rev))){
System.out.println("Yes! It is palindrom");
}else{
System.out.println("No! It is not palindrom");
}
}
}