Java程序检查给定字符串是否为Pangram(潘格拉姆)
在本文中,我们将了解如何检查给定的字符串在Java中是否为Pangram(潘格拉姆)。如果一个字符串包含字母表中的所有字符(不区分大小写),则该字符串为Pangram字符串。这里,我们将使用两种不同的方法:使用main()方法和使用封装。
问题陈述
给定一个字符串,编写一个Java程序来查找该字符串是否为Pangram。
输入
Input string: Abcdefghijklmnopqrstuvwxyz
输出
Yes, the string is a pangram
不同的方法
以下是检查给定字符串是否为Pangram的步骤:
使用main()方法
以下是使用main()方法检查给定字符串是否为Pangram的步骤:
- 首先,我们将创建一个名为Pangram的类,其中包含一个常量size = 26和一个辅助方法isLetter(char ch)来检查字符是否为字母。
- 在main()中,定义输入字符串"Abcdefghijklmnopqrstuvwxyz",打印它,并将其转换为小写。
- 创建一个大小为26的布尔数组is_true,以标记每个字母是否已找到。
- 使用for循环遍历input_string中的每个字母,并在is_true中标记其位置。
- 遍历is_true并使用if else语句进行条件检查,如果任何条目为false,则结果为false。
- 如果所有字母都存在,则打印"Yes, the string is a pangram";否则,打印"No, the string is not a pangram"。
示例
在这里,我们将所有操作绑定到main()方法下:
public class Pangram {
static int size = 26;
static boolean isLetter(char ch) {
if (!Character.isLetter(ch))
return false;
return true;
}
public static void main(String args[]) {
String input_string = "Abcdefghijklmnopqrstuvwxyz";
System.out.println("The string is defined as: " +input_string);
int string_length = input_string.length();
input_string = input_string.toLowerCase();
boolean[] is_true = new boolean[size];
for (int i = 0; i < string_length; i++) {
if (isLetter(input_string.charAt(i))) {
int letter = input_string.charAt(i) - 'a';
is_true[letter] = true;
}
}
boolean result;
for (int i = 0; i < size; i++) {
if (!is_true[i])
result = false;
}
result = true;
if (result)
System.out.println("\nYes, the string is a pangram");
else
System.out.println("\nNo, the string is not a pangram");
}
}
输出
The string is defined as: Abcdefghijklmnopqrstuvwxyz Yes, the string is a pangram
使用封装
以下是使用封装检查给定字符串是否为Pangram的步骤:
- 首先,我们将定义Pangram类,其中size = 26和一个辅助方法isLetter(char ch)。
- 创建check_alphabets(String input_string, int string_length)方法,将输入转换为小写,在布尔数组中标记字母,并验证所有字母是否存在。
- 在main()中,初始化输入字符串"Abcdefghijklmnopqrstuvwxyz",打印它,并调用check_alphabets方法。
- 根据check_alphabets的返回值,打印"Yes, the string is a pangram"或"No, the string is not a pangram"。
示例
在这里,我们将操作封装到函数中,展示了面向对象编程:
public class Pangram {
static int size = 26;
static boolean isLetter(char ch) {
if (!Character.isLetter(ch))
return false;
return true;
}
static boolean check_alphabets(String input_string, int string_length) {
input_string = input_string.toLowerCase();
boolean[] is_true = new boolean[size];
for (int i = 0; i < string_length; i++) {
if (isLetter(input_string.charAt(i))) {
int letter = input_string.charAt(i) - 'a';
is_true[letter] = true;
}
}
for (int i = 0; i < size; i++) {
if (!is_true[i])
return false;
}
return true;
}
public static void main(String args[]) {
String input_string = "Abcdefghijklmnopqrstuvwxyz";
System.out.println("The string is defined as: " +input_string);
int string_length = input_string.length();
if (check_alphabets(input_string, string_length))
System.out.println("\nYes, the string is a pangram");
else
System.out.println("\nNo, the string is not a pangram");
}
}
输出
The string is defined as: Abcdefghijklmnopqrstuvwxyz Yes, the string is a pangram
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP