Hello everyone,i need help here(urgent!!):
here is what my program for:
1.user will enter a string not more than 300 words.
2.change string to lowercase
3.count each word occurrence

example:
This is a test. This program will count number of each word occurrence.
this = 2
is = 1
a = 1
test = 1
program = 1
will = 1
count = 1
number = 1
of = 1
each = 1
word = 1
occurrence = 1

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main ()
{
int tokenCount,i;
char string[7500];

char *pToken;

printf("Please enter a paragraph, maximum of 300 words: \n");
gets (string);

//change uppercase to lowercase
for (i=0; i<7500; ++i) string[i] = tolower (string[i]);

//to break the string
tokenCount = 0;		//to count number of words
pToken = strtok(string," ");


	while (pToken != NULL) 
	{
		tokenCount ++;
		printf ("[%2d]  %s\n",tokenCount, pToken);
		pToken = strtok(NULL,", . ; : # ! @ $ % ^ & * ( ) - _ = + { } [ ] | ` ~ < > / ? \\ 1 2 3 4 5 6 7 8 9 0");
	}



		return 0;
	}

>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.

actually i am stuck with the code.

1. I tried to make 2 dimensional array but i kept getting error and warning.
2. I don't know how to make the program combine with strcmp. (to compare and count the occurrence)

I would be grateful if you can do the code for me with my existing code. Please help me fix the code.
p/s: I still new with C. So there is still much i don't know.

>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.

#include <stdio.h>
#define WORDS_SIZE 300
#define FREQUENCY_SIZE 300

int main()
{

int answer;
int rating;
char words;
char string[7500];

int frequency [FREQUENCY_SIZE] = {0};
printf("Please enter a paragraph, maximum of 300 words: \n");
gets (string);

int words[WORDS_SIZE] = {gets (string)};



for (answer =0; answer<WORDS_SIZE; answer++)
    {
     ++frequency[words[answer]];
     }

     printf ("%s%17s\n", "rating","frequency");

     for (rating = 1; rating<FREQUENCY_SIZE; rating++){
     printf ("%6d%17d\n", rating,frequency[rating]);

    }

    return 0;
    }

i have made a new code.it's different but
can you please tell me which part is wrong? i still got 2 error.
i want the program to read the data from user.
what should i put to get it right?

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;

This is what i've done. Actually i've finished it on last monday but i'm too busy with school.
I still didn't use fgets tho. :p Maybe somebody could help me change it to fgets.
I'm quite busy right now,but i'll try to change it to fgets. In the meantime, if you're kind enough please change it for me. ;p

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main ()
{
int tokenCount;
int wordCount;
int i;
char stringarray[6300];
char *pToken;
int isMatched;
char words[300][20];
int wordFrequency [300];
int count;

printf ("***********************************************************************************\n");
printf ("*                                                                                 *\n");
printf ("*                                                                                 *\n");
printf ("*                                                                                 *\n");
printf ("*\t\t\t::Welcome to our program::                                *\n\n\n\n");

printf ("\t Program Name : Word Frequency Counter                                           \n");
printf ("\t Description  : Aims to count the frequency of word                              \n\n\n");
printf ("Instructions:\n\n");
printf ("     1.This program will count number of word occurrence in your sentence.\n");
printf ("     2.Number of words must not be longer than 300 words.\n\n");
printf ("-----------------------------------------------------------------------------------\n\n");

printf ("Please enter a string/paragraph or sentence: \n\n");
gets(stringarray);
printf ("\n\n\n\n");


if (stringarray != NULL)
    for (i=0; i<6300; ++i) stringarray[i] = tolower (stringarray[i]);

i=strlen(stringarray);
printf ("\nNumber of character in your sentence is %d\n",i);
printf ("-----------------------------------------------------------------------------------\n\n");


wordCount = 0;
tokenCount = 0;
pToken = strtok(stringarray," ");


    while (pToken != NULL) 
    {

        isMatched = 0;
        count = 0;

        while (count < wordCount && !isMatched)
            if (strcmp (pToken, words[count])==0)
            {   ++wordFrequency[count];
                isMatched = 1;
            }

            else
                ++count;

            if (!isMatched)
            {   

                strcpy( words[wordCount] , pToken);
                wordFrequency[wordCount]= 1;
                ++wordCount;
            }
            tokenCount ++;
            printf ("[%2d]  %s\n",tokenCount, pToken);


            pToken = strtok(NULL,", . ; : # ! @ $ % ^ & * ( ) - _ = + { } [ ] | ` ~ < > / ? \\ 1 2 3 4 5 6 7 8 9 0");



    }
    printf ("\nNumber of words is: %3d\n\n", tokenCount);
    printf ("***********************************************************************************\n\n");

    printf ("   -----------------------------------------\n");
    printf ("   | WORDS                |     FREQUENCY  |\n");
    printf ("   -----------------------------------------\n");

    for (count = 0; count<wordCount;  ++count)
    {   printf ("   | %-20s = %10d     |\n", words[count],wordFrequency[count]);}

    printf ("   -----------------------------------------\n\n\n");
    printf ("\t\t\t-->>End of Program<<--\n");
    printf ("\t\t<<Thank You for Using This Program!!>>\n\n\n");



return 0;
}

> 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
}

Thanks for the help Salem, Aia and to Narue-kun. :]
This is what I've done so far.I think I've nothing more to add.
But if there is something i missed,error i made or anything to add to increase my prog robustness,notice me.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main ()
{
int tokenCount;
int wordCount;
int i;
char stringarray[6300];
char *pToken;
int isMatched;
char words[300][20];
int wordFrequency [300];
int count;

printf ("***********************************************************************************\n");
printf ("*                                                                                 *\n");
printf ("*                                                                                 *\n");
printf ("*                                                                                 *\n");
printf ("*\t\t\t::Welcome to our program::                                *\n\n\n\n");

printf ("\t Program Name : Word Frequency Counter                                           \n");
printf ("\t Description  : Aims to count the frequency of word                              \n\n\n\n\n\n\n");
printf (" Instructions:\n\n");
printf ("     1.This program will count number of word occurrence in your sentence.\n");
printf ("     2.Number of words must not be longer than 300 words.\n\n\n");
printf ("-----------------------------------------------------------------------------------\n\n");

printf ("Please enter a string/paragraph or sentence: \n\n");
fgets( stringarray, sizeof stringarray, stdin );
{ char *p = strchr(stringarray,'\n'); if ( p ) *p = '\0'; }
printf ("\n\n");


if (stringarray, sizeof stringarray, stdin != NULL)
	for (i=0; i<6300; ++i) stringarray[i] = tolower (stringarray[i]);

i=strlen(stringarray);
printf ("\nNumber of character in your sentence is %d\n",i);
printf ("-----------------------------------------------------------------------------------\n\n");


wordCount = 0;
tokenCount = 0;
pToken = strtok(stringarray," ");


	while (pToken != NULL) 
	{

		isMatched = 0;
		count = 0;

		while (count < wordCount && !isMatched)
			if (strcmp (pToken, words[count])==0)
			{	++wordFrequency[count];
				isMatched = 1;
			}
		
			else
				++count;

			if (!isMatched)
			{	

				strcpy(	words[wordCount] , pToken);
				wordFrequency[wordCount]= 1;
				++wordCount;
			}
			tokenCount ++;
			printf ("[%2d]  %s\n",tokenCount, pToken);
		
		
			pToken = strtok(NULL,", . ; : # ! @ $ % ^ & * ( ) - _ = + { } [ ] | ` ~ < > / ? \\ 1 2 3 4 5 6 7 8 9 0");

		
	
	}
	printf ("\nNumber of words is: %3d\n\n", tokenCount);
	printf ("***********************************************************************************\n\n");

	printf ("   -----------------------------------------\n");
	printf ("   | WORDS                |     FREQUENCY  |\n");
	printf ("   -----------------------------------------\n");

	for (count = 0; count<wordCount;  ++count)
	{	printf ("   | %-20s = %10d     |\n", words[count],wordFrequency[count]);}
	
	printf ("   -----------------------------------------\n\n\n");
	printf ("\t\t\t-->>End of Program<<--\n");
	printf ("\t\t<<Thank You for Using This Program!!>>\n\n\n");



return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.