In this blog post, we will write a Java program that counts the number of words in a given string. This program uses string manipulation techniques to split the input string into individual words and then count them. It provides a simple and efficient way to determine the word count in a text. Let's explore the code and see how it works!
In this post, we discuss three different approaches to writing a Java program to count a number of words in given input String.
Approach 1: Using the split() Method
In this program, we use the split() method to split the input string into words based on one or more whitespace characters (\\s+). The resulting words are stored in an array, and the length of the array gives the word count.
public class WordCount {
public static void main(String[] args) {
String inputString = "Java is a programming language.";
// Removing leading and trailing whitespaces
inputString = inputString.trim();
// Splitting the string into words
String[] words = inputString.split("\\s+");
// Counting the number of words
int wordCount = words.length;
System.out.println("Number of words: " + wordCount);
}
}
Output:
Number of words: 5
Approach 2: Using StringTokenizer
In this program, we use the StringTokenizer class to tokenize the input string by default delimiters, which include whitespace characters. The countTokens() method provides the word count.
import java.util.StringTokenizer;
public class WordCount {
public static void main(String[] args) {
String inputString = "Java is a programming language.";
// Creating a StringTokenizer object
StringTokenizer tokenizer = new StringTokenizer(inputString);
// Counting the number of words
int wordCount = tokenizer.countTokens();
System.out.println("Number of words: " + wordCount);
}
}
Output:
Number of words: 5
Approach 3: Using Regular Expressions
In this program, we will use a regular expression (\\b\\w+\\b) to define a pattern that matches individual words. A Matcher object is created based on the pattern, and the find() method is used in a loop to find all word occurrences. The count is incremented for each match.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordCount {
public static void main(String[] args) {
String inputString = "Java is a programming language.";
// Creating a pattern to match words
Pattern pattern = Pattern.compile("\\b\\w+\\b");
// Creating a matcher object
Matcher matcher = pattern.matcher(inputString);
// Counting the number of words
int wordCount = 0;
while (matcher.find()) {
wordCount++;
}
System.out.println("Number of words: " + wordCount);
}
}
Conclusion
In this blog post, we explored different ways to write a Java program to count the number of words in a given string. These approaches provide flexibility in implementing word-counting logic based on personal preferences or specific requirements. You can choose the approach that best suits your needs and integrate it into your Java projects.
Related Java String Programs with Output
- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge Two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains Only Digits
- How to Check if the String Contains Only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Swap Two Strings Without Using Third Variable
Comments
Post a Comment
Leave Comment