开发者

Problem finding the ratio of frequency of a word to total no of words in a text file in c

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#define NULL 0



int main()
{
    char name[20],c;
    int nw=0;
    int j=0;
    int t=0;
    char s[] = "newas";  // find the frequency of this word in abc.txt
    char p[5];
   开发者_Go百科 FILE *fpt;
    //printf("Enter the name of file to be checked:- ");
    //gets(name);
    fpt=fopen("abc.txt","r");
    if (fpt==NULL)
    {
        printf("ERROR - can/'t open file %s",name);
        getch();
        exit(0);
    }
    else
    {
        while ((c=getc(fpt))!=EOF)
        {
            switch(1)
            {
                case 1:
                    if (c==' ')
                    {
point:
                        while((c=getc(fpt))==' ');

                        if (c!=' ')
                            nw=nw+1;
                        // if(c==' ')
                            // nw--;
                        if(j < 5)
                            p[j++] = c;
                        printf("\n %c ",p[j]);
                        if(j == 5)
                        {
                            if(p == s)
                            {
                                t++;
                                j = 0;    
                            }
                        }
                    }

                    if(c==' ')
                    {
                        j = 0;
                        goto point;
                    }
            }
        }
    }
    printf("\n The no. of words is %d. ",nw);
    printf("\n Freq of words %s is %d. ",s,t);
    getch();
}

The code above code is giving right answer for the total number of words but is not giving the right value of frequency of a particular word [s in given code], please comment on this, how to calculate frequency of a particular word in a text file.


As you are including iostream.h, I would guess this is supposed to be some form of C++, not C. If so, this is how you do word frequencies:

#include <iostream>
#include <map>
#include <string>
#include <fstream>

using namespace std;

typedef map <string, int> FreqMap;

int main() {

    FreqMap frequencies;
    ifstream ifs( "words.txt" );
    string word;

    while( ifs >> word ) {
        frequencies[ word ] += 1;
    }

    for ( FreqMap::const_iterator it = frequencies.begin();
            it != frequencies.end(); ++it ) {
        cout << it->first << " " << it->second << "\n";
    }
}


This code that looks for the interesting word:

                        if(p == s)
                        {
                            t++;
                            j = 0;    
                        }

Is wrong. You can't compare strings like that in C, this only compares the pointer values, not the characters being pointed at (the string's content).

Assuming the rest of the code is set up properly, so that p really points at a true string, you can do this:

if(strcmp(p, s) == 0)
{
  t++;
  j = 0;
}

That requires that p points at a fully 0-terminated string, if it points at some character in the middle of a line the above won't work.


I'm not exactly answering the question, but this is some feedback that might help you on your way...

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#define NULL 0



int main()
{
/* 
 * GIVE YOUR VARIABLES NAMES THAT MAKE SENSE
 * j, t, c, s, nw are meaningless to anybody picking up the code 
 */
    char name[20],c;
    int nw=0;
    int j=0;
    int t=0;
    char s[] = "newas";  // find the frequency of this word in abc.txt
/* 
 * Personally, I'd tend to have p as an array of 6, so that it's the same size as
 * s and I'd initialize it to "", so that it's got a null terminator.
 */
    char p[5]; 
    FILE *fpt;

    fpt=fopen("abc.txt","r");
    if (fpt==NULL)
    {
        printf("ERROR - can/'t open file %s",name);
        getch();
        exit(0);
    }

/*
 * you don't need an else here... the other flow has already terminated */
 */

    else
    {
        while ((c=getc(fpt))!=EOF)
        {
/*
 * What is the point of this switch statement?  It may as well say if(true)
 */
            switch(1)
            {
                case 1:
                    if (c==' ')
                    {
/*
 * If you start using goto's in your code, it's usually a good sign that there's
 * something wrong
 */
point:
/*
 * It's hard to follow what you're doing because your variables don't have names
 * and your code has no clear intent.  If the while loop was in a function
 * 'SkipToNextWord', the intent would be clearer, which would make it easier to find
 * issues.  What happens if there is a space at the end of your file?
 */
                        while((c=getc(fpt))==' ');

/*
 * 'c' is never going to equal ' ', if it did, you'd still be in the while loop
 */
                        if (c!=' ')
                            nw=nw+1;
                        // if(c==' ')
                            // nw--;
                        if(j < 5)
                            p[j++] = c;
                        printf("\n %c ",p[j]);
/*
 * This as written, could be a compound if statement...
 *     if(j == 5 && p == s)
 */
                        if(j == 5)

                        {
/*
 * However, it looks like you're trying to do a string comparison?
 *     if(strncmp(p, s, sizeof(s)-1)==0)
 */
                            if(p == s)
                            {
                                t++;
/* 
 * This 'j=0' should be outside of the inner if, otherwise if there isn't a match
 * you don't reset j to 0
 */
                                j = 0;    
                            }
                        }
                    }

/* 
 * If you have a six letter word in your file, j is never reset to
 * 0 and next time round the loop, you're not going to collect the
 * letters correctly
 */
                    if(c==' ')
                    {
                        j = 0;
                        goto point;
                    }
            }
        }
    }
    printf("\n The no. of words is %d. ",nw);
    printf("\n Freq of words %s is %d. ",s,t);
    getch();
}


I think the following code will answer your question:

#include <stdio.h>
#include <conio.h>

int main(int argc, char* argv[])
{
    char*   name = "abc.txt";
    char*   word = "newas";
    FILE*   fpt = fopen(name, "rt");
    int     c;
    int     nw = 0;
    int     t = 0;
    int     i;

    if (fpt == NULL)
    {
        printf("ERROR - can't open file %s\n", name);
        getch();
        return 0;
    }

    while ((c = getc(fpt)) != EOF)
    {
        // Skip spaces
        if (c == ' ')
            continue;   

        // Increase num of words
        nw++;

        // Check match
        i = 0;
        while ((c != EOF) && (c != ' ') && ((char)c == word[i]) && (word[i] != '\0'))
        {
            c = getc(fpt);
            i++;
        }

        if (((c == ' ') || (c == EOF)) && (word[i] == '\0'))
            t++; 

        // skip this word
        while ((c != EOF) && (c != ' '))
            c = getc(fpt);
    }

    fclose(fpt);

    printf("\n The no. of words is %d.\n", nw);
    printf("\n Freq of words %s is %d.\n", word, t);
    getch();

    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜