Write a program that reads in a line of text and computes the frequency of the w
ID: 3676006 • Letter: W
Question
Write a program that reads in a line of text and computes the frequency of the words in the text. Assume that the input contains words separated by white spaces, comma, period, or exclamation point. Sample input/output: Name your program frequency. c. Assume input is no longer than 1000 characters. Assume each word is no more than 50 characters. Store a word into a string array when it is first encountered. Create a parallel integer array to hold a count of the number of times that each particular word appears in the input. If the word appears in the input multiple time, do not add it to the string array, but make sure to increment the corresponding word frequency counter in the parallel integer array. You may use any string library functions such as strtok, strcmp, and strcpy. To read a line of text, use the read_line function in the lecture notes.For MAC (gcc terminal). Please show the output.Explanation / Answer
/* C/C++ program to count no of words from given input string. */
#include <stdio.h>
#define OUT 0
#define IN 1
// returns number of words in str
unsigned countWords(char *str)
{
int state = OUT;
unsigned wc = 0; // word count
// Scan all characters one by one
while (*str)
{
// If next character is a separator, set the
// state as OUT
if (*str == ' ' || *str == ' ' || *str == ' '|| *str == ',')
state = OUT;
// If next character is not a word separator and
// state is OUT, then set the state as IN and
// increment word count
else if (state == OUT)
{
state = IN;
++wc;
}
// Move to next character
++str;
}
return wc;
}
// Driver program to tes above functions
int main(void)
{
printf("No of words: %u ", countWords(str));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.