Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads in a line of text and computes the frequency of thewo

ID: 3676953 • Letter: W

Question

Write a program that reads in a line of text and computes the frequency of thewords in the text. Assume that the input contains words separated by white spaces,comma, period, or exclamation point.

Sample input/output:
Input: I came, I saw, I conquered!

Output:I   3came   1saw   1conquered 1

1) Name your program frequency.c.

2) Assume input is no longer than 1000 characters. Assume each word is no morethan 50 characters.

3) Store a word into a string array when it is first encountered. Create a parallelinteger array to hold a count of the number of times that each particular wordappears in the input. If the word appears in the input multiple time, do not add itto the string array, but make sure to increment the corresponding word frequencycounter in the parallel integer array.

4) You may use any string library functions such as strtok, strcmp, andstrcpy.

5) To read a line of text, use the read_line function

int read_line(char str[], int n){

int ch, i = 0;
while ((ch = getchar()) != ' ')if (i < n)

str[i++] = ch;

str[i] = '';   /* terminates string */
return i;        /* number of characters stored */}

Explanation / Answer

#include <stdio.h>

#include <string.h>

int main()

{

int count = 0, frequency = 0, i, j = 0, k, space = 0;

char input[100], copy[50][100], str1[20], word_freq[50][100];

  

printf("Input ");

scanf(" %[^ ]s", input);

  

for (i = 0;i<strlen(input);i++)

{

if ((input[i] == ' ')||(input[i] == ', ')||(input[i] == '.')||(input[i] == '!'))

{

space++;

}

}

for (i = 0, j = 0, k = 0;j < strlen(input);j++)

{

if ((input[j] == ' ')||(input[j] == 44)||(input[j] == 46))

{

copy[i][k] = '';

i++;

k = 0;

}

else

copy[i][k++] = input[j];

}

k = 0;

for (i = 0;i <= space;i++)

{

for (j = 0;j <= space;j++)

{

if (i == j)

{

strcpy(word_freq[k], copy[i]);

k++;

count++;

break;

}

else

{

if (strcmp(word_freq[j], copy[i]) != 0)

continue;

else

break;

}

}

}

for (i = 0;i < count;i++)

{

for (j = 0;j <= space;j++)

{

if (strcmp(word_freq[i], copy[j]) == 0)

frequency++;

}

printf("%s - %d ", word_freq[i], frequency);

frequency = 0;

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote