Skip to content

Instantly share code, notes, and snippets.

@davidmerwin
Last active November 22, 2023 02:53
Show Gist options
  • Save davidmerwin/f59a18a562765083c1efbe5624be5753 to your computer and use it in GitHub Desktop.
Save davidmerwin/f59a18a562765083c1efbe5624be5753 to your computer and use it in GitHub Desktop.
Counts the number of times each word appears in a given paragraph and prints them to their console.

Count Words in Paragraph.c

Preview:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // added to use tolower()

// Function prototype
int *count_words_in_paragraph(char *paragraph);
void print_word_counts(int *word_counts);

int main() {
    // Get the paragraph from the user
    char paragraph[1024];
    printf("Enter a paragraph: ");
    fgets(paragraph, sizeof(paragraph), stdin);

    // Count the words in the paragraph
    int *word_counts = count_words_in_paragraph(paragraph);

    // Print the word counts
    print_word_counts(word_counts);

    // Free the memory allocated for the word counts
    free(word_counts);

    return 0;
}

int *count_words_in_paragraph(char *paragraph) {
    // Convert the paragraph to lowercase and split it into words
    char *words = strdup(paragraph);
    for (char *word = strtok(words, " "); word != NULL; word = strtok(NULL, " ")) {
        // Convert the word to lowercase
        for (int i = 0; word[i] != '\0'; i++) {
            word[i] = tolower(word[i]);
        }
    }

    // Create a dictionary to store the word counts
    int *word_counts = calloc(256, sizeof(int)); // 256 represents all possible ASCII characters

    // Iterate over the words and count their occurrences
    for (char *word = strtok(words, " "); word != NULL; word = strtok(NULL, " ")) {
        // Increment the count for the word
        word_counts[word]++;
    }

    // Free the memory allocated for the words
    free(words);

    // Return the dictionary of word counts
    return word_counts;
}

void print_word_counts(int *word_counts) {
    // Iterate over the dictionary and print the word counts
    for (int i = 0; i < 256; i++) {
        if (word_counts[i] > 0) {
            printf("%c: %d\n", i, word_counts[i]);
        }
    }
}
Associated Context
Type Code Snippet ( .c )
Associated Tags Word Counting String Manipulation Dictionary Data Structure Character Pointers Memory Allocation Text Processing Console Output Function Arguments Word Count Dictionary Lowercase Conversion String Tokenization User Input Memory Deallocation Standard Input/Output Main Function
💡 Smart Description Counts the number of times each word appears in a given paragraph and prints them to their console.
This code snippet takes a paragraph as input from the user, counts the number of times each word appears in the paragraph, and then prints the word counts. It converts the paragraph to lowercase, splits it into words, and uses a dictionary to store the
🔎 Suggested Searches function to count number of times in a paragraph
How to iterate over words and count occurrences using Python
function to print word counts from paragraphs with lowercase characters
program to get the list of sentences by character including uppercase letters
Code to find all consecutive numbers within an paragraph
How to count the number of times each word appears in a paragraph in C
C code to count word occurrences in a paragraph
C function to count word frequencies in a paragraph
Code to convert a paragraph to lowercase and split it into words in C
How to create a dictionary to store word counts in C
Related Links https://www.techiedelight.com/find-first-non-repeating-character-string-one-traversal/
https://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python
https://stackoverflow.com/questions/7427101/simple-argparse-example-wanted-1-argument-3-results
https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm
https://www.geeksforgeeks.org/strtok-strtok_r-functions-c-examples/
https://www.learn-c.org/en/Strings
https://www.guru99.com/c-strings.html
Related People Jack Cheng, David Merwin
Sensitive Information No Sensitive Information Detected
Shareable Link https://davidmerwin.pieces.cloud/?p=ad4f4d9bd1
@davidmerwin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment