My order is wrong in the code. the output is not in the right order it should be
ID: 3804862 • Letter: M
Question
My order is wrong in the code. the output is not in the right order it should be and it includes periods and exclamation points. how do i fix this?
#include <stdio.h>
#include <string.h>
int main()
{
char string[1000], words[50][1000], temp[1000];
int i=0, j=0, k=0, n=0, count;
//uses fgets to read input from the user with max 1000
printf("Enter your input string: ");
fgets(string, 1000, stdin);
//adds null terminating character in end of string
string[strlen(string) - 1] = '';
//while loop to put string into a parallel integer array
while (string[i] != '')
{
if (string[i] == ' ' ||string[i] == ',' || string[i] == ‘.’ || string[i] == ‘!’)
{
//adds null terminating character when end of word
words[j][k] = '';
//sets k=0 and j keeps on adding
k = 0;
j++;
}
else
{
words[j][k++] = string[i];
}
i++;
}
// adds null terminating character when done in two dimensional words
words[j][k] = '';
n = j;
//sorts the words into temp
for (i = 0; i < n; i++)
{
//copies words[i] into temp to swap them later
strcpy(temp, words[i]);
for (j = i + 1; j <= n; j++)
{
// if words[i] and [j] are not equal they get swapped
if (strcmp(words[i], words[j]) > 0)
{
strcpy(temp, words[j]);
strcpy(words[j], words[i]);
strcpy(words[i], temp);
}
} //inner for
} //outer for
printf("Frequency of words: ");
i = 0;
//finds the frequency of each word and put them into counter variable
while (i <= n) {
count = 1;
if (i != n) {
for (j = i + 1; j <= n; j++) {
if (strcmp(words[i], words[j]) == 0) {
count++;
}
}//for
}
//i was getting commas getting included in the output so i included this statement so it doesnt get included
if(strcmp(words[i], "") != 0)
//count indicates frequency of word at words[i]
// -9 in print f so it is spaced as the output in project PDF shows
printf("%-9s %d ", words[i], count);
//goes to the next word
i = i + count;
}
printf(" ");
return 0;
}
the right output is under the "expected:" portion. thank you very much for the help
Enter your input string: Frequency of words: came conquered! saw Expected: Enter the sentnece: I came, I saw, I conquered Output came saW conquered Enter your input string: Frequency of words: know more read the things will you Expected the more you read the more things you will know Output the more 2 you read things will knowExplanation / Answer
Below is the required code:-
Thank you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.