Java 程序计算字符串中的总单词数

编写一个 Java 程序,使用示例来计算字符串中的总单词数。在此字符串单词计数示例中,我们首先使用 for 循环迭代 strTWords。在循环内部,为了使代码简单,我们将每个字符分配给 TWord_ch (TWord_ch = strTWords.charAt(i))。接下来,我们使用 If 语句。

我们在第一个 if 条件 (TWord_ch == ‘ ‘ )|| TWord_ch == ‘\t’) 中检查字符是否为空格或制表符。如果为真,我们将 totalWords 值加一。接下来,我们将 totalWords 打印为结果。

import java.util.Scanner;

public class CountTotalStrWords1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String strTWords;
		int i, totalWords;
		totalWords = 1;
		char TWord_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Count Words =  ");
		strTWords = sc.nextLine();
		
		for(i = 0; i < strTWords.length(); i++)
		{
			TWord_ch = strTWords.charAt(i);
			if((TWord_ch == ' ' ) || TWord_ch == '\t') {
				totalWords++;
			}
		}		
		System.out.println("\nTotal Number of Words  =  " + totalWords);
	}
}
Java Program to Count Total Words in a String

使用 if else 的 Java 程序计算字符串中的总单词数

在此 Java 程序中,首先检查给定的文本是否为空。如果不为空,我们使用 split 函数将 strTWords 分割并分配给一个字符串数组。稍后,我们打印字符串 数组的长度。

import java.util.Scanner;

public class CountTotalStrWords2 {
private static Scanner sc;
public static void main(String[] args) {
String strTWords;

sc= new Scanner(System.in);

System.out.print("\nPlease Enter Text = ");
strTWords = sc.nextLine();

if(strTWords == null || strTWords.isEmpty()) {
System.out.println("\nPlease enter the Non-Empty String");

}
else {
String[] words = strTWords.split("\\s+");
System.out.println("\nTotal Number of Words = " + words.length);
}
}
}
Please Enter Text =  learn java programming

Total Number of Words =  3

使用 StringTokenizer

下面提到的程序接受文本并使用 StringTokenizer 计算字符串中的总单词数。

import java.util.Scanner;
import java.util.StringTokenizer;

public class CountTotalStrWords3 {
private static Scanner sc;
public static void main(String[] args) {
String strTWords;

sc= new Scanner(System.in);

System.out.print("\nPlease Enter Words = ");
strTWords = sc.nextLine();

if(strTWords == null || strTWords.isEmpty()) {
System.out.println("\nPlease enter the Non-Empty String");

}
else {
StringTokenizer tk = new StringTokenizer(strTWords);
System.out.println("\nTotal Number of Words = " + tk.countTokens());
}
}
}
Please Enter Words =  abc working at abc company

Total Number of Words  =  5

使用函数

这个计算字符串单词数的 程序与上面的程序相同。在这里,我们使用 Java 函数将字符串单词计数逻辑分开。

import java.util.Scanner;

public class CountTotalStrWords4 {
private static Scanner sc;
public static void main(String[] args) {
String strTWords;
int totalWords;

sc= new Scanner(System.in);

System.out.print("\nPlease Enter Text = ");
strTWords = sc.nextLine();

totalWords = CountTotalWords (strTWords);
System.out.println("\nTotal Number of Words = " + totalWords);
}
public static int CountTotalWords (String strTWords) {
int i, totalWords;
totalWords = 1;

for(i = 0; i < strTWords.length(); i++)
{
if((strTWords.charAt(i) == ' ' && strTWords.charAt(i - 1) != ' ')
|| strTWords.charAt(i) == '\t')
{
totalWords++;
}
}
return totalWords;
}
}
Please Enter Text =  learn java programming at tutorial gateway

Total Number of Words  =  6