Java 给定字符串中的单词计数

Java 给定字符串中的单词计数

字符串是Java中的一个类,用于存储在双引号内的一系列字符。这些字符作为String类型的对象。本文的目的是编写Java程序,以计算给定字符串中的单词。可以使用字符串操作技术来解决给定字符串的单词计数问题。在Java面试中,关于字符串操作的问题非常频繁,因此,正确理解这个问题非常重要。

给定字符串的Java程序中的单词计数

在跳转到示例程序之前,让我们通过一个示例来了解问题陈述。

输入

String = "You are on Tutorials Point website";

输出

Number of words in the given string: 6

我们可以观察到字符串中的单词是由空格(‘ ‘)分隔的,并且为了向Java编译器传达这些信息,我们将检查当前索引处的字符是空格还是字母。如果它是空格且下一个字符是字母,则将计数器变量递增以指定我们遇到了一个单词。

现在,让我们讨论一些Java程序来计算给定字符串中的单词数量。

示例1

以下示例说明了如何使用while循环打印给定字符串中的单词数量。

方法

  • 首先,初始化一个要计算单词数量的字符串。

  • 然后,声明一个计数器变量来存储单词数量。

  • 接下来,使用while循环遍历字符串的每个字符。在这个循环内部,使用’charAt()’方法定义一个if块来检查空格。如果遇到空格,将递增计数器。

  • 最后,打印结果并退出。

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

示例2

在这个示例中,我们将使用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

示例3

这是另一个使用内置方法’split()’来检查给定字符串中单词数量的Java程序。我们将使用’\s+’作为split()方法的分隔符参数来将单词与空格分隔开,并使用字符串的长度属性来检查单词的数量。

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循环。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程