Write a Java Program to Count Total Words in a String with an example. In this count number of words in a string example, we first used for loop to iterate strTWords. Inside the loop, to keep the code simple, we assigned (TWord_ch = strTWords.charAt(i)) each character to TWord_ch. Next, we used the If statement.
We check whether the character is empty or tab space within the first if condition (TWord_ch == ‘ ‘ )|| TWord_ch == ‘\t’). If true, we increment the totalWords value. Next, we print the totalWords as the result.
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 using if else
In this Java program, first, check whether the given text is empty. If not, we used the split function to split strTWords and assigned them to a string array. Later, we print the length of a string array.
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
Using StringTokenizer
The below mentioned program accepts the text and count the Total number of Words in a String using 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
Using functions
This Program to Count the number of Words in a String is the same as the above. Here, we separated the string word count logic using Java functions.
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