Reputation: 33
I want to count how many words occurs in a given sentence. I am using C Programming language. It can not count the last word. In a given string, It counts each word how many times occurs. If there is a sentence like red green blue blue green blue
, then the program should count red 2 green 2 and blue 3
. But in my case, it does not count as blue 3
. rather than count blue 2
and then blue 1
:
red 1
green 2
blue 2
blue
1
My code:
#include <stdio.h>
#include <string.h>
int main(void)
{
int count = 0, c = 0, i, j = 0, k, space = 0;
char str[1000], p[500][1000], str1[200], ptr1[500][1000];
char *ptr;
fgets(str, sizeof(str), stdin);
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] = '\0';
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\n", ptr1[i], c);
c = 0;
}
return 0;
}
Upvotes: 2
Views: 1565
Reputation: 1
#define MAXWORD 100
#define MAXSTRING 10000
void WordCount()
{
/*Decalaration */
char *wordArray[MAXWORD] = { 0 };
int count[MAXWORD] = {0};
char inputString[MAXSTRING];
int wordCount=0;
/*Reading data from input stream*/
fgets(inputString, sizeof(inputString), stdin);
/*Remove trailing new line char*/
inputString[strlen(inputString) -1] = 0;
/*Init string tokenizer*/
char *wordPointer = strtok(inputString, " ");
while (wordPointer)
{
int len = strlen(wordPointer);
int found = 0;
for (int i = 0; i < wordCount; i++)
{
/*check if word already processed then incrment word count*/
if (strncmp(wordArray[i], wordPointer, len)==0)
{
count[i]++;
found = 1;
break;
}
}
if (!found)
{
/*Allocate memory for string and copy for future comparision*/
wordArray[wordCount] = (char*)malloc(len + 1);
strncpy(wordArray[wordCount], wordPointer, len);
wordArray[wordCount][len] = 0;
count[wordCount]++;
wordCount++;
}
wordPointer = strtok(NULL, " ");
}
/* print words and their frequency*/
for (int i = 0; i < wordCount; i++)
{
printf("%s - %d \n", wordArray[i], count[i]);
}
}
Upvotes: -1
Reputation: 12742
fgets
appends line feed character (\n
) to the str
Hence your str
will contain
str = "red green blue blue green blue\n"
Thus blue
is not matching to blue\n
and counting blue\n
as different word.
And same is exactly shown on your output
red 1
green 2
blue 2
blue //see 1 is printed on next line
1
Thus trim the \n
as below.
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n')
str[len - 1] = '\0';
Upvotes: 2