#Need help on word frequency project

13 messages · Page 1 of 1 (latest)

unique quest
#

This is the prompt I was given, I'm not sure how to iterate through the array to find duplicates of strings, add the frequencies to it's own array, and print the output like it's example. How could I do this? Prompt below:

Write a program that reads a list of words. Then, the program outputs those words and their frequencies. The input begins with an integer indicating the number of words that follow. Assume that the list will always contain fewer than 20 words, and each word contains fewer than 10 characters.

Ex: If the input is:

5 hey hi Mark hi mark
the output is:

hey - 1
hi - 2
Mark - 1
hi - 2
mark - 1
Hint: Use two arrays, one array for the strings and one array for the frequencies.

woven breachBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

river jetty
#

!hw

woven breachBOT
# river jetty !hw
Monke
We Don't Do Your Homework

Welcome to Together C & C++ :wave:

We won't do your homework for you (#rules) but we are happy to help you learn and point you in the right direction.

Please send what you have so far and ask a specific question.

urban drift
#

Some guidance I can provide....

You could use a function like strtok() to delimit the words on whitespace.
But strtok() is a stateful function, so professionals frown on its use.
I don't think that should stop a beginner doing some homework assignment using strtok(), as long as they are informed.

Another way I would probably prefer is to use repeated sscanf(), especially in combination with the %n parameter which informs the caller how many characters were "consumed" in reading the string.

#

;compile

#include <stdio.h>
#include <string.h>

int main()
{
    static const char* sentence = "one two three four";

    int nchars = 0;
    char word[10];

    for (const char *pos = sentence, *end = pos + strlen(sentence); pos < end; pos += nchars + 1) {
        if (sscanf(pos, "%s%n", word, &nchars) == 1) {
            puts(word);
        }
    }
}
digital fableBOT
#
Program Output
one
two
three
four
urban drift
#

Ideally should be using sscanf_s() but gcc refuses to comply to this standard.

#

The strtok() way...

#

;compile

#include <stdio.h>
#include <string.h>

int main()
{
    char sentence[] = "one two three four";
    static const char* whitespace = " \t"; // space or tab

    const char* pos = strtok(sentence, whitespace);
    while (pos != NULL) {
        puts(pos);
        pos = strtok(NULL, whitespace);
    }
}
digital fableBOT
#
Program Output
one
two
three
four
urban drift
#

Note that you cannot use strtok() on a string literal. It must be a mutable string variable. That's because strtok() modifies the original string.
It replaces all delimiters in said string with a NULL char.
So delimiting on spaces makes a string like "one two three four\0" (terminating NULL shown only for clarity) become "one\0two\0three\0four\0".