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 迴圈。

更新於: 2024年8月30日

22K+ 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.