C++ Program to Count Word Occurrences in Text

This article provides two C++ program solutions to count the number of times a search word appears in a given text. Both programs utilize input from the user, allowing them to specify the search word and the text to analyze.

Scenario 1: No Punctuation

This program assumes the text input will not contain any punctuation marks.

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char search_word[20], word[20];
    int count = 0;
    
    cout << 'Enter the search word: '; 
    cin >> search_word;
    cout << 'Enter some words (end with *): '; 
    
    while (cin >> word)
    {
        if (word[strlen(word) - 1] == '*')
        {
            word[strlen(word) - 1] = '\0';
            if (strcmp(word, search_word) == 0)
                count++;
            break;
        }
        else
        {
            if (strcmp(word, search_word) == 0)
                count++;
        }
    }
    
    cout << "The word '" << search_word << "' appeared " << count << " times." << endl;
    
    return 0;
}

Scenario 2: With Punctuation and Case Insensitivity

This program handles text input that may contain punctuation marks ('.', '!', '?') and accounts for case differences (treating 'cat' and 'CAT' as the same word).

#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

int main()
{
    char search_word[20], word[20];
    int count = 0;
    
    cout << 'Enter the search word: '; 
    cin >> search_word;
    cout << 'Enter some words (end with *): '; 
    
    while (cin >> word)
    {
        if (word[strlen(word) - 1] == '*')
        {
            word[strlen(word) - 1] = '\0';
            if (strcasecmp(word, search_word) == 0)
                count++;
            break;
        }
        else
        {
            int len = strlen(word);
            if (ispunct(word[len - 1]))
                word[len - 1] = '\0';
            if (strcasecmp(word, search_word) == 0)
                count++;
        }
    }
    
    cout << "The word '" << search_word << "' appeared " << count << " times." << endl;
    
    return 0;
}

Explanation:

  1. Input: Both programs first prompt the user to enter the search word and then to enter a series of words ending with a '*'.
  2. Word Processing: The code loops through each input word. For the first scenario (no punctuation), the program checks if the word is equal to the search word. In the second scenario (with punctuation), it handles punctuation by removing it and then performs a case-insensitive comparison using strcasecmp.
  3. Counting: Each time a match is found, the count variable is incremented.
  4. Output: After processing all the words, the program prints the total number of occurrences of the search word in the text.

These examples demonstrate how to use C++ string manipulation and comparison functions to analyze text and count word occurrences. By understanding these concepts, you can build more complex programs for text processing and analysis.


原文地址: https://www.cveoy.top/t/topic/oZSB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录