Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

word counter

rox383rox383 Member Posts: 2
any insight would be helpful...point in the right direction as to what I am missing

yes as anyone with a clue could figure out this is an assignment... so please don't write it for me. I would like to figure it out but I seem to be looping endlessly. thanks in advance.

my objective:
write a program (console) in C to count the number of times every word occurs in a textfile, the program shall print the word that occure most frequently and the number of times it appears in the file.

when I debug, i see it gets to k=i and exits the for loop, then I had planned it to go back to the while loop, so I could get the next "string" in the textfile, I can't quite figure out it just loops and increases the word_count. how do I get the next string in the text and compare it to the current array.

my code as thus far

[code]
/* include libriaries */
#include /* includes fscanf, etc. */
#include /* includes exit function */
#include /* includes strcpy and _strcmp functions */

/* define constants */
#define STR_SIZE 31 /* allows us to store 30 characters, plus the EOS */
#define NUM_WORDS 600 /* allows us to store up to 600 unique words in the file */







/* some declared variables */
FILE *inp; /* file pointer variable */
char str[STR_SIZE]; /* temporary string variable to scan from file */
char words[NUM_WORDS][STR_SIZE]; /* internal word store for the program [database] */
int word_count[NUM_WORDS]; /* array of individual count of each word stored */
int i; /* index variable */
int j; /* index variable */
int k; /* index variable */
int found_it; /* indicates when a word in database */
int highest; /* highest frequecy word in file */



/* this code will comes somewhere after opening the file and begining to scan for data */
/* Name: main */
int
main(int argc, char *argv[])
{
/* input file variables */

int status;
i = 0;
j = 0;
/* print intro message */
if (argc < 2)
{
printf("xxxxx Fall 2008 P3, xxxxx, xxxxxx
");
exit(0);
}

/* process file */
else if (argc >=2)
{
/* open file */
inp=fopen(argv[1], "r");
if (inp==(FILE *)NULL)
{
/* print message if unable to open designated file */
printf("Unable to open file %s. Please select another file to open. Thank you.
", argv[1]);
exit(0);
}


else
{
/* read file */

status = fscanf(inp,"%s",str);
while(status != EOF)
{
found_it = 0; /* reset the flag variable */

/* storing the very first word scanned from file */
if(j == 0)
{
strcpy(words[i], str);
word_count[i] = 1;

++i; /* increment to next position for storing */
++j; /* scanned first item, so increment */

}
else if(j > 0)
{
/* searching our stored strings for a matching word */
for(k = 0; k < i; k++)
{
if(strcmp(words[k], str) == 0)
{
word_count[k] += 1; /* add one to the word count */
found_it = 1; /* set the flag variable */
k = i; /* force exit from for-loop */
}
else if(found_it == 0) /* if didn't find, then add new word */
{
strcpy(words[i], str); /* add new word */
word_count[i] = 1; /* add one to its count */
++i; /* increment for the next potential entry */
}
}
}
}
status = fscanf(inp,"%s",str);
/* close file */
fclose(inp);

j = i;
highest = word_count[0];

/* look for the higest freqency word */
for(i = 0; i < j; ++i)
{
if(highest > word_count[i])
highest = word_count[i];
}

/* print the specified output statement */
printf("The word, %s, occurred %.4d times in %s.
",words[i],highest,argv[1]);

return(0);


}
}
}

[code]





Comments

  • LundinLundin Member Posts: 3,711
    I think this is your problem:

    [code]status = fscanf(inp,"%s",str);

    while(status != EOF)
    {
    for(...)
    {
    }
    }

    status = fscanf(inp,"%s",str);[/code]

    status is never modified from within the loop.

    I would suggest doing like this instead:

    [code]while(fscanf(inp,"%s",str) != EOF)
    {
    }[/code]

    ---

    A little programming tip is to consider giving your variables intiuitive names, the code is a bit hard to understand.

    For example, instead of this:

    ++i; /* increment to next position for storing */
    ++j; /* scanned first item, so increment */

    you could use variables like this:

    word_pos++;
    scanned_items++;

    Comments are always good, but now the code suddenly speaks for itself.
  • rox383rox383 Member Posts: 2
    : I think this is your problem:
    :
    : [code]: status = fscanf(inp,"%s",str);
    :
    : while(status != EOF)
    : {
    : for(...)
    : {
    : }
    : }
    :
    : status = fscanf(inp,"%s",str);[/code]:
    :
    : status is never modified from within the loop.
    :
    : I would suggest doing like this instead:
    :
    : [code]: while(fscanf(inp,"%s",str) != EOF)
    : {
    : }[/code]:
    :
    : ---
    :
    : A little programming tip is to consider giving your variables
    : intiuitive names, the code is a bit hard to understand.
    :
    : For example, instead of this:
    :
    : ++i; /* increment to next position for storing */
    : ++j; /* scanned first item, so increment */
    :
    : you could use variables like this:
    :
    : word_pos++;
    : scanned_items++;
    :
    : Comments are always good, but now the code suddenly speaks for
    : itself.



    thanks for the help and tips. I will implement them in my code.
Sign In or Register to comment.