Write a program that reads in a line of text and computes the frequency of the w
ID: 3677062 • 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: Input: I came, I saw, I conquered! Output: I 3 Came 1 saw 1 conquered 1 1)Name your program frequency. c.2)Assume input is no longer than 1000 characters. Assume the input contains no more than 1000 words. Assume each word is no more than 50 characters. 3)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. 4)You may use any string library functions such as strtok, strcmp, and strcpy. 5)To read a line of text, use the read_line function in the lecture notes.Explanation / Answer
1. Name of the Program
frequency.c
#include <stdio.h>
#include <string.h>
void main()
{
int count = 0, c = 0, i, j = 0, k, space = 0;
char str[200], p[100][150], str1[40], ptr1[70][200];
char *ptr;
printf("Enter the string to the environment ");
scanf(" %[^ ]s", str);
printf("string length is %d ", strlen(str));
for (i = 0;i<strlen(str);i++)
{
if ((str[i] == ' ')||(str[i] == ', ')||(str[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0;j < strlen(str);j++)
{
if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))
{
p[i][k] = '';
i++;
k = 0;
}
else
p[i][k++] = str[j];
}
k = 0;
for (i = 0;i <= space;i++)
{
for (j = 0;j <= space;j++)
{
if (i == j)
{
strcpy(ptr1[k], p[i]);
k++;
count++;
break;
}
else
{
if (strcmp(ptr1[j], p[i]) != 0)
continue;
else
break;
}
}
}
for (i = 0;i < count;i++)
{
for (j = 0;j <= space;j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
printf("%s -> %d times ", ptr1[i], c);
c = 0;
}
}
2. For the second program no longar than 1000 characters and input contians no more than 1000 words
int count = 0, c = 0, i, j = 0, k, space = 0;
char str[1000], p[100][150], str1[40], ptr1[70][200];
char *ptr;
for (i = 0;i<strlen(str);i++)
{
if ((str[i] == ' ')||(str[i] == ', ')||(str[i] == '.') && (str[i]<=1000))
{
space++;
}
}
here remaining program is same
3.
store a word into a string array when it is first encountered
.....
for (i = 0;i < count;i++)
{
for (j = 0;j <= space;j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
printf("%s -> %d times ", ptr1[i], c);
c = 0;
}
4.
Using of string functions strcmp, strlen, strcpy
for (i = 0;i<strlen(str);i++)
{
if ((str[i] == ' ')||(str[i] == ', ')||(str[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0;j < strlen(str);j++)
{
if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))
{
p[i][k] = '';
i++;
k = 0;
}
else
p[i][k++] = str[j];
}
k = 0;
for (i = 0;i <= space;i++)
{
for (j = 0;j <= space;j++)
{
if (i == j)
{
strcpy(ptr1[k], p[i]);
k++;
count++;
break;
}
else
{
if (strcmp(ptr1[j], p[i]) != 0)
continue;
else
break;
}
}
}
for (i = 0;i < count;i++)
{
for (j = 0;j <= space;j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
5. To read a line of text using the read_line
In this it reads the line of text from user input
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.