
How To Count Word Occurrences In A String: Python, C++, Java & More!
Ever needed to know how many times a specific word appears in a text? Whether you're analyzing data, processing text, or just curious, counting word occurrences is a fundamental task in programming. This guide provides clear, concise examples in multiple languages to help you master this skill.
Why Count Word Occurrences?
Understanding the frequency of words in a string has many practical uses. Here's why you should care:
- Text Analysis: Identify the most important keywords in a document.
- Data Processing: Clean and prepare text data for machine learning.
- Search Engines: Improve search result relevance by understanding query frequency.
- Content Optimization: Refine writing for clarity and impact by analyzing word usage.
Method 1: Simple String Splitting
This approach splits the string into words and then iterates through the resulting list, incrementing a counter for each match. Let's explore implementations across several languages.
C++
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int countOccurrences(string str, string word) {
stringstream ss(str);
string token;
int count = 0;
while (ss >> token) {
if (token == word) {
count++;
}
}
return count;
}
int main() {
string text = "GeeksforGeeks A computer science portal for geeks";
string targetWord = "portal";
cout << "Occurrences of '" << targetWord << "': " << countOccurrences(text, targetWord) << endl;
return 0;
}
C
Java
Python 3
C#
Javascript
PHP
Method 2: Leveraging the count()
Function
Python offers a built-in function count()
that simplifies this task even further when working with lists. This method leverages the ease and readability of Python for a concise solution.
Python 3
Method 3: Regular Expressions
For more complex scenarios, such as counting whole words only or ignoring case, regular expressions provide a powerful and flexible solution.
Python 3
- re.escape(word): Ensures that any special characters in the target word are properly escaped.
- r'\b': Matches a word boundary, ensuring that only whole words are counted.
- re.IGNORECASE: Performs case-insensitive matching.
Choose Your Method
- For basic counting needs, the simple splitting method is efficient and easy to understand.
- Python's
count()
function offers a more concise solution when working with lists. - Regular expressions provide the most flexibility for advanced scenarios.
No matter the method you choose, understanding how to count word occurrences empowers you to analyze and manipulate text data effectively.