>i need help here(urgent!!)
First thing's first, urgent for you doesn't mean urgent for us. Deal with it.
>gets (string);
Don't ever use gets. It's evil, and there's no way to make it less evil. Use fgets instead.
Your count isn't counting the right thing. You're counting the number of words, not the occurrence of each word. What you're looking to build is a frequency table, where all of the unique words are saved with an individual count starting at 1. Then when you find another word that's already in the list, you increment the count.
Unfortunately, there's not really a simple way to do that without using a suitable data structure, and I'm reasonably sure your project doesn't allow stuff like that. So you're stuck with arrays, which are slower, and more cumbersome.
Give it a try first, and if you have trouble, come back and I'll give you some sample code.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>1. I tried to make 2 dimensional array but i kept getting error and warning.
It's hard to help you without knowing what those errors and warnings were.
>2. I don't know how to make the program combine with strcmp. (to compare and count the occurrence)
Probably something along these lines:
for ( i = 0; i < nwords; i++ ) {
if ( strcmp ( words[i], new_word ) == 0 ) {
++count[i]; /* count and words would be parallel arrays */
break;
}
else if ( nwords < MAX_WORDS ) {
strcpy ( words[nwords++], new_word );
break;
}
}
>I would be grateful if you can do the code for me with my existing code.
I guess you won't be grateful, because we don't do other people's homework here.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Your program doesn't compile because you have a char variable name: char words;
and another integer variable array with the same name: int words[WORDS_SIZE] ;
Why do you insists in using gets (string); here and here int words[WORDS_SIZE] = {gets (string)}; when Narue already told you: "Don't ever use gets. It's evil, and there's no way to make it less evil. Use fgets instead."
If you keep ignoring given advise, why to post for it at all?
Take a look at the links posted here , for examples and information in how to implement
fgets instead of gets;
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
> gets(stringarray);
Would be replaced initially by
fgets( stringarray, sizeof stringarray, stdin );
But this will leave the \n in the result, if one was found. If that would cause a problem, then it can be removed with { char *p = strchr(stringarray,'\n'); if ( p ) *p = '\0'; }
Also, you should also check the return result of fgets, with something like
if ( fgets( stringarray, sizeof stringarray, stdin ) != NULL ) {
// all the stuff which uses the input
}
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953