Java程序计算给定字符串中的单词数
字符串是Java 中的一个类,它存储用双引号括起来的一系列字符。这些字符充当字符串类型对象。本文的目的是编写 Java 程序来计算给定字符串中的单词数。计算给定字符串的单词可以使用字符串操作技术来解决。在 Java 面试中,字符串操作方面的问题非常频繁,因此,正确理解此问题非常重要。
问题陈述
编写一个 Java 程序来计算给定字符串中的单词数。
输入
"You are on Tutorials Point website"
输出
Number of words in the given string: 6
我们可以观察到字符串中的单词由空格 (' ') 分隔,为了将此信息指示给 Java 编译器,我们将检查当前索引处的字符是空格还是字母。如果它是空格并且下一个字符是字母,我们将增加计数器变量以指定我们遇到了一个单词。
现在,让我们讨论一些 Java 程序来计算给定字符串中的单词数。
计算给定字符串中单词的不同方法
以下是计算给定字符串中单词的不同方法:
使用 While 循环计数单词
以下是使用 while 循环计算给定字符串中单词的步骤:
- 首先,我们将通过声明和分配字符串变量 msg 来初始化一个字符串。
- 之后,我们将把给定的字符串打印到控制台。
- 通过将整数变量 total 设置为 1 来初始化计数器,以开始计数单词。
- 我们将使用 while 循环遍历字符串中的每个字符。
- 在循环内部,使用 if 条件检查当前字符是否为空格,并且下一个字符是否不是空格。
- 如果条件为真,则将 total 增加 1。
- 递增循环变量 I。
- 打印字符串中单词的总数。
以下示例说明了如何使用 while 循环打印给定字符串中单词的数量。
public class Example1 {
public static void main(String args[]) {
// initializing a string
String msg = "Tutorials Point Welcomes You!!";
System.out.println("The given String is: " + msg);
// initial count of the words
int total = 1;
// loop variable
int i = 0;
// while loop to count the number of words
while (i < msg.length()) {
// checking if the current character is space or not
if ((msg.charAt(i) == ' ') && (msg.charAt(i + 1) != ' ')) {
total++; // incrementing the word count
}
i++; // incrementing loop variable
}
// printing the result
System.out.println("Number of words in the given string: " + total);
}
}
输出
The given String is: Tutorials Point Welcomes You!! Number of words in the given string: 4
使用 for 循环计数单词
以下是使用 for 循环计算给定字符串中单词的步骤:
- 将整数变量 total 设置为 1 以开始计数单词。
- 使用 for 循环遍历字符串中的每个字符。
- 在循环内部,使用 if 条件检查当前字符是否为空格,并且下一个字符是否不是空格。如果条件为真,则将 total 增加 1。
- 打印字符串中单词的总数。
示例
在此示例中,我们将使用 for 循环 而不是 while 循环 来检查给定字符串的单词数。
public class Example2 {
public static void main(String[] args) {
// initializing a string
String msg = "Tutorials Point Welcomes You!!";
System.out.println("The given String is: " + msg);
// initial count of the words
int total = 1;
// for loop to count the number of words
for (int i = 0; i < msg.length(); i++) {
// checking if current character is space or not
if ((msg.charAt(i) == ' ') && (msg.charAt(i + 1) != ' ')) {
total++; // incrementing the word count
}
}
// printing the result
System.out.println("Number of words in the given string: " + total);
}
}
输出
The given String is: Tutorials Point Welcomes You!! Number of words in the given string: 4
使用 split 方法计数单词
以下是使用 split() 方法计算给定字符串中单词的步骤:
- 通过声明和分配字符串变量 msg 来初始化一个字符串。
- 将给定的字符串打印到控制台。
- 使用split() 方法根据空格将字符串划分为单词。
- 使用结果数组的 length 属性确定单词的总数。
- 通过打印字符串中单词的总数来显示结果。
示例
这是另一个 Java 程序,它将使用名为“split()”的内置方法检查给定字符串中单词的数量。我们将使用“\s+”分隔符作为 split() 方法的参数,以将单词与空格分隔开,并且为了检查单词的数量,我们将使用字符串的 length 属性。
public class Example3 {
public static void main(String[] args) {
// initializing a string
String msg = "Tutorials Point Welcomes You!! ";
System.out.println("The given String is: " + msg);
// To Split the string into words
String[] arrayStr = msg.split("\s+");
// To Count the number of words
int totalWord = arrayStr.length;
// printing the result
System.out.println("Number of words in the given string: " + totalWord);
}
}
输出
The given String is: Tutorials Point Welcomes You!! Number of words in the given string: 4
结论
在本文中,我们学习了什么是字符串以及如何在 Java 中检查给定字符串的单词数量。对于此字符串操作,我们使用了 String 的内置方法,如 charAt() 和 split(),以及 while 和 for 循环。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP