user3599630
user3599630

Reputation: 17

How to count different words from a text on C?

I wrote this code but it looks so bad. This function prints how many times is repeated each word. Is there another way to do the same function simple and easy to understand?

int diffrentwords(FILE *myinput){
    int i,j,words[40],difwords=0;
char text[40][40];
char a;

rewind(myinput);
for(i=0;i<40;i++){
    words[i]=0;
    for(j=0;j<40;j++)
        text[i][j]=' ';
}
    for (i=0;i<40;i++){
    if(feof(myinput))
        break;
    for(j=0;j<40;j++){
        a=fgetc(myinput);
        if(a!=' ')
            text[i][j]=a;
        else{
            text[i][j]='\0';
            break;
        }
    }
}
text[i][0]='\0';
i=0;

for(i=0;text[i][0]!='\0';i++)
    for(j=i+1;text[j][0]!='\0';j++)
        if(text[j][0]!=' ' && !(strcmp(text[i],text[j]))){
            words[i]++;
            text[j][0]=' ';
        }
for(i=0;text[i][0]!='\0';i++)
    if(text[i][0]!=' '){
        printf("%s \t %d\n",text[i],words[i]+1);
        difwords++;
    }
printf("\n");
return difwords;

}

Upvotes: 0

Views: 708

Answers (2)

user3386109
user3386109

Reputation: 34829

Step 1 is to read all of the words from the file. fscanf can be used for this purpose. The nice thing about fscanf is that it will read entire words, handle all of the white space issues, and detect the end-of-file. So the code to read the file looks like this

int count;
char word[40][40];
FILE *fp;

if ( (fp = fopen( "input.txt", "r" )) == NULL )
    exit( 1 );

for ( count = 0; count < 40; count++ )
{
    if ( fscanf( fp, "%39s", word[count] ) != 1 )
        break;
}

fclose( fp );

Step 2 is to figure out how many unique words there are in the file. A word is counted as unique if none of the words before it in the array are the same. For example, a word at index 9 is counted as unique if it doesn't match any of the words at index 0 through 8.

int i, j, unique, repeated;

unique = 0;
for ( i = 0; i < count; i++ )
{
    repeated = 0;
    for ( j = 0; j < i; j++ )
        if ( strcmp( word[i], word[j] ) == 0 )
        {
            repeated = 1;
            break;
        }

    if ( !repeated )
    {
        unique++;
        printf( "%s\n", word[i] );
    }
}

printf( "The number of unique words is %d\n", unique );

Upvotes: 2

Muhammad Hewedy
Muhammad Hewedy

Reputation: 30058

First you should have the function for one and only one purpose.

You cannot have a function that read from the file and do count. ( single responsibilty)

Second, lets rethink again... you will have some function that return a line by line from the file.

Then you will have another function that read from the first one and try find space chars so it can consider a word.

Another function will take each word and search in some array of struct {char* word; int count;} and if it found the word, it increment the count, otherwise it sets it to 1.

By this you will have more organized and readable code.

Upvotes: 0

Related Questions