Menu
Coddy logo textTech

Recap: Word Frequency Counter

Part of the Logic & Flow section of Coddy's C journey — lesson 63 of 63.

challenge icon

Challenge

Easy

Create a C program that implements a word frequency counter to analyze text input. Your program should:

  1. Write a function named countWordOccurrences that:
    • Takes two char* parameters: sentence and targetWord
    • 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)
  2. Write a function named extractWord that:
    • Takes a char* sentence, an integer startIndex, and a char* word parameter
    • Returns an integer representing the index after the extracted word
    • Extracts a single word starting from startIndex until a space or null terminator is found
    • Stores the extracted word in the word parameter
    • Adds a null terminator to complete the extracted word
  3. Write a function named analyzeText that:
    • 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)
  4. In the main function:
    • Declare a character array inputSentence of size 200
    • Declare a character array searchWord of size 50
    • Read a sentence from input (may contain multiple words separated by spaces)
    • Read a target word to search for
    • Call the analyzeText function and print the total word count in this exact format: Total words: [count]
    • Call the countWordOccurrences function 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

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