How could I get my program to check if a word is a palindrome irrespective of the case entered by the user

huangapple go评论79阅读模式
英文:

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(&quot;Please enter a word&quot;);
        String ori = sc1.nextLine();
        isPailindrome(ori);
        if (isPailindrome(ori))
            System.out.println(ori + &quot;is a Pailindrome&quot;);
        else
            System.out.println(ori + &quot;is NOT a Pailindrome&quot;);
    }
    public static boolean isPailindrome(String ori) {
        int i = 0;
        int j = ori.length() - 1;
        while (i &lt; 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(&quot;Please enter a word&quot;);
  String ori = sc1.nextLine();
  ori = ori.toLowerCase();
  isPailindrome(ori);
  if (isPailindrome(ori))
 }
 System.out.println(ori + &quot;is a Pailindrome&quot;);
} else {
 System.out.println(ori + &quot;is NOT a Pailindrome&quot;);
}
}
public static boolean isPailindrome(String ori) {
 int i = 0;
 int j = ori.length() - 1;
 while (i &lt; 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 &lt; len &gt;&gt;1; i++) {
			if (Character.toLowerCase(str.charAt(i)) != 
					Character.toLowerCase(str.charAt(len - i - 1))) {
				return false;
			}
		}
		return true;
	}

</details>



huangapple
  • 本文由 发表于 2020年4月7日 01:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61065175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定