Recap: Word Frequency Counter
Part of the Logic & Flow section of Coddy's C journey — lesson 63 of 63.
Challenge
EasyCreate a C program that implements a word frequency counter to analyze text input. Your program should:
- Write a function named
countWordOccurrencesthat:- Takes two
char*parameters:sentenceandtargetWord - Returns an integer representing the number of times the target word appears in the sentence
- Uses a loop to process the sentence character by character
- Identifies word boundaries using spaces as delimiters
- Extracts each word from the sentence and compares it with the target word using
strcmp() - Counts exact matches (case-sensitive comparison)
- Takes two
- Write a function named
extractWordthat:- Takes a
char*sentence, an integerstartIndex, and achar*word parameter - Returns an integer representing the index after the extracted word
- Extracts a single word starting from
startIndexuntil a space or null terminator is found - Stores the extracted word in the
wordparameter - Adds a null terminator to complete the extracted word
- Takes a
- Write a function named
analyzeTextthat:- Takes a
char*sentence parameter - Returns an integer representing the total number of words in the sentence
- Counts words by identifying spaces as separators
- Handles multiple consecutive spaces correctly (treats them as single separators)
- Takes a
- In the main function:
- Declare a character array
inputSentenceof size 200 - Declare a character array
searchWordof size 50 - Read a sentence from input (may contain multiple words separated by spaces)
- Read a target word to search for
- Call the
analyzeTextfunction and print the total word count in this exact format:Total words: [count] - Call the
countWordOccurrencesfunction and print the result in this exact format:Occurrences of '[searchWord]': [count] - Calculate and print the word frequency as a percentage with exactly 1 decimal place in this exact format:
Frequency: [percentage]% - Determine and print the frequency category:
- If frequency is 0.0%: print
Category: Not found - If frequency is greater than 0.0% but less than 20.0%: print
Category: Rare - If frequency is between 20.0% and 50.0% (inclusive): print
Category: Common - If frequency is greater than 50.0%: print
Category: Frequent
- If frequency is 0.0%: print
- Declare a character array
This challenge tests your mastery of string manipulation, tokenization, and text processing. You'll practice breaking sentences into individual words, comparing strings for exact matches, and implementing multiple functions that work together to analyze text data. The program demonstrates real-world text processing techniques essential for many C applications.
Try it yourself
#include <stdio.h>
#include <string.h>
// TODO: Write your extractWord function here
// TODO: Write your countWordOccurrences function here
// TODO: Write your analyzeText function here
int main() {
char inputSentence[200];
char searchWord[50];
// Read input sentence
fgets(inputSentence, sizeof(inputSentence), stdin);
// Remove newline character if present
inputSentence[strcspn(inputSentence, "\n")] = '\0';
// Read search word
scanf("%s", searchWord);
// TODO: Write your code below
// Call analyzeText function and store total word count
// Call countWordOccurrences function and store occurrences
// Calculate frequency percentage
// Determine frequency category
// Output results in the required format
// printf("Total words: %d\n", totalWords);
// printf("Occurrences of '%s': %d\n", searchWord, occurrences);
// printf("Frequency: %.1f%%\n", frequency);
// printf("Category: %s\n", category);
return 0;
}All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions