Counting Recurring words

I've got a small problem, I'm unable to count the recurring words. it counts on its own and the number calculated is wrong. here is my code.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"story.h"

int main()
{
int i;
char word[20];
printf("Convert Word Uppercase\n");
printf("======================\n\n");

for ( i =0 ;story[i] != '\0' ;i++)
{
printf("%c",story[i]);
}
printf("\n\nPlease enter the re-curring word you want to convert:");
scanf("%s", word);
i=500;
printf("\nScanning for the word(%s): %03d", word, i);
for (i = 500; i>=0;i--)
{
printf("\b\b\b%03d",i);
}
printf("\n\n");
printf("The total number of recurring word \"%s\" is %d ",word,i);
printf("\n\n");
system("pause");
return 0;
}
Your problem is as yet unclear. Are you counting the number of words? or converting every occurrence of the word to uppercase? or both?

Is story[] a string containing all the words to search? (As in, "The quick brown fox jumps over the lazy dog.")?

Line 18 has an overflow error. Make sure you specify the maximum length of the string to read:
scanf( "%19c\n", word );

Better yet, use fgets():
1
2
3
4
5
6
7
8
9
10
11
12
char* p;
/* Get the input string */
p = fgets( word, 20, stdin );

/* Complain on error */
if (p == NULL) complain();

/* Remove the trailing newline. */
/* Complain if the word is larger than 19 characters. */
p = strchr( word, '\n' );
if (p == NULL) complain();
*p = '\0';

(Don't use gets(). That is just as bad as using %s.)

In your loop to count words, you don't do anything except print the value of i.

Topic archived. No new replies allowed.