My program find the amount of specific word such as "whale" and "thee", but ignore "thee,", "whale....", and so on. How can I contain them?

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

int main(){
    FILE *md,*out;
    char CHA[255];
    char cha[255];

        cha[255]=tolower(CHA[255]);

    char thee[]="thee";

    char chapter[]="chapter";
    char whale[]="whale";
    char meet[]="meet";

    int th=0;
    int ch=0;
    int wh=0;
    int me=0;

    md=fopen("moby_dick.txt","r");
    out=fopen("output.txt","w");

    if(md==NULL){
            printf("Empty text");
                        return 1;
        }
            while(feof(md)==0){
                fscanf (md,"%s",&cha);
                if(strcmp(cha,thee)==0 ){
                    th++;
                }
                if(strcmp(cha,chapter)==0){
                                    ch++;
                                }
                if(strcmp(cha,whale)==0){
                                    wh++;
                                }
                if(strcmp(cha,meet)==0){
                                    me++;
                                }
            }

    printf("%d\n",th);
    printf("%d\n",ch);
    printf("%d\n",wh);
    printf("%d\n",me);

    fprintf(out,"%d\n",th);
    fprintf(out,"%d\n",ch);
    fprintf(out,"%d\n",wh);
    fprintf(out,"%d\n",me);

    fclose(md);
    fclose(out);

}

Your problem description needs work. Maybe you wanted to write:

  1. Count the number of whale occurances in the text unless preceeded by the word "thee".

But you didn't write that so as it stands the word count would be zero's if I took your words literally.

Use regular expressions instead of fixed string comparison.
By the way, ' cha[255]=tolower(CHA[255]);' is not correct.

Hi Mooyeon,
As I understand you want to search for a whole word.
If so, your problem is caused by the fact that the fscanf function stops reading at the end of the word, this means it stops reading when it finds a whitespace or a punctuation mark.

You must explcitly check the next letter after find matching pattern.

Best Regards
Ruediger

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.