英文:
How could I get my program to check if a word is a palindrome irrespective of the case entered by the user
问题
import java.util.Scanner;
public class Pailindrome {
    public static void main(String[] args) {
        Scanner sc1 = new Scanner(System.in);
        System.out.println("Please enter a word");
        String ori = sc1.nextLine();
        isPailindrome(ori);
        if (isPailindrome(ori))
            System.out.println(ori + " is a Palindrome");
        else
            System.out.println(ori + " is NOT a Palindrome");
    }
    public static boolean isPailindrome(String ori) {
        ori = ori.toLowerCase();  // Convert input to lowercase
        int i = 0;
        int j = ori.length() - 1;
        while (i < j) {
            if (ori.charAt(i) != ori.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}
英文:
import java.util.Scanner;
public class Pailindrome {
    public static void main(String[] args) {
        Scanner sc1 = new Scanner(System.in);
        System.out.println("Please enter a word");
        String ori = sc1.nextLine();
        isPailindrome(ori);
        if (isPailindrome(ori))
            System.out.println(ori + "is a Pailindrome");
        else
            System.out.println(ori + "is NOT a Pailindrome");
    }
    public static boolean isPailindrome(String ori) {
        int i = 0;
        int j = ori.length() - 1;
        while (i < j) {
            if (ori.charAt(i) != ori.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}
The code works perfectly I'm just confused how I will get it to work irrespective of the case
inputted by the user. For example aBba is a palindrome but It says it's not in the code I've done. I
would like any help if possible thanks.
答案1
得分: 3
将输入内容传入 toUpper() 函数;这样,当你检查是否为回文时,所有字符都将转换为大写。
String ori = scr.nextLint();
if (isPalindrome(ori.toUpperCase()))
//做一些操作
英文:
Take the input and call toUpper(); that way when you check to see if it is a palindrome, all of the characters are uppercase.
String ori = scr.nextLint();
if(isPalindrome(ori.toUpperCase()))
//do something
答案2
得分: 3
你可以在开始处理之前将所有字母转换为小写。
你可以编写自己的函数或使用 toLowerCase() 字符串函数。
import java.util.Scanner;
public class Pailindrome {
 public static void main(String[] args) {
  Scanner sc1 = new Scanner(System.in);
  System.out.println("请输入一个单词");
  String ori = sc1.nextLine();
  ori = ori.toLowerCase();
  isPalindrome(ori);
  if (isPalindrome(ori)) {
   System.out.println(ori + "是一个回文");
  } else {
   System.out.println(ori + "不是一个回文");
  }
 }
 
 public static boolean isPalindrome(String ori) {
  int i = 0;
  int j = ori.length() - 1;
  while (i < j) {
   if (ori.charAt(i) != ori.charAt(j)) {
    return false;
   }
   i++;
   j--;
  }
  return true;
 }
}
英文:
You can convert all of the letters to lowerCase before you start the processing.
You can write your own function or use toLowerCase() String function.
import java.util.Scanner;
public class Pailindrome {
 public static void main(String[] args) {
  Scanner sc1 = new Scanner(System.in);
  System.out.println("Please enter a word");
  String ori = sc1.nextLine();
  ori = ori.toLowerCase();
  isPailindrome(ori);
  if (isPailindrome(ori))
 }
 System.out.println(ori + "is a Pailindrome");
} else {
 System.out.println(ori + "is NOT a Pailindrome");
}
}
public static boolean isPailindrome(String ori) {
 int i = 0;
 int j = ori.length() - 1;
 while (i < j) {
  if (ori.charAt(i) != ori.charAt(j)) {
   return false;
  }
  i++;
  j--;
 }
 return true;
}
答案3
得分: 1
convert all the cases to lowercase/uppercase before checking the palindrome
isPailindrome(ori.toLowerCase());
英文:
Convert all the cases to lowercase/uppercase before checking the palindrome
isPailindrome(ori.toLowerCase());
答案4
得分: 1
从两端放大并根据需要调整大小。
public static boolean isPalindrome(String str) {
    int len = str.length();
    for (int i = 0; i < len >> 1; i++) {
        if (Character.toLowerCase(str.charAt(i)) !=
            Character.toLowerCase(str.charAt(len - i - 1))) {
            return false;
        }
    }
    return true;
}
英文:
Zoom in from both ends and adjust the case as required.
	public static boolean isPalindrome(String str) {
		int len = str.length();
		for (int i = 0; i < len >>1; i++) {
			if (Character.toLowerCase(str.charAt(i)) != 
					Character.toLowerCase(str.charAt(len - i - 1))) {
				return false;
			}
		}
		return true;
	}
</details>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论