-1

I currently work on a text file where it has a fixed number of words. And all I want is to count the occurrence of a word in a text file and output its density. I have 266 words inside a text file and I want to output the count and density of words and the word itself.

e.g. (sample.txt)
The quick brown fox jumps over the lazy brown dog.

Output:
**Count     Density     Word**
   2         0.2%       The
   2         0.2%       brown

OP's code:

#define DELIM " "
#include <stdio.h>

int main()
{
    int c; 
    int count = 0;
    FILE *file, *temp;
    char line[200];
    char *result, *result2;
    file = fopen("sample.txt", "r"); 
    temp = fopen("temp.txt", "w"); 

    if (file)
    { 
        while ((c = getc(file)) != EOF) 
        { 
            if (c == '.' || c == '(' || c == ')' || c == ',' || c == ':' || c == '-' || c == '’')
            {
                fputc(putchar(' '), temp);
                continue;
            } else
            {
                count = count + 1;
                fputc(c, temp);
            }
        }
        fclose(file); 
        fclose(temp); 
        temp = fopen("temp.txt", "r"); 
        while (fgets(line,200,temp) != NULL)
        {
abelenky
  • 63,815
  • 23
  • 109
  • 159
ajdeguzman
  • 1,223
  • 3
  • 16
  • 27
  • There is no question here. We do not write your code for you. Show code! – abelenky Jul 02 '13 at 02:17
  • sounds like homework. but if not, may I suggest another language like Python for this? Might be more well suited for this problem than C. – gkayling Jul 02 '13 at 02:17
  • The literal in `... c == '’')` is curious. The character isn't a regular single quote; that would be represented by `'\''`. Not necessarily wrong, but quite possibly pointing towards problems (such as single quote not being handled, and/or other variants on the theme of single quote not being handled, and maybe double quotes and variants on double quotes not being handled). – Jonathan Leffler Jul 02 '13 at 03:09
  • @JonathanLeffler You're right. I already corrected that :) Thanks – ajdeguzman Jul 02 '13 at 03:18

1 Answers1

3
  1. Use a search-optimized data structure such as a binary tree or a hash table, indexed by a word;
  2. Since you are case-insensitive, convert the word to lower or upper case before storing;
  3. At each node, store the count;
  4. When you add a new word, its count is 1;
  5. When you add an existing word, you increase its count by 1;
  6. When you process any word, increase a global counter.

Now you can traverse your tree or hash table and output the word counts. At the same time, you can output the density by dividing the word count by the global count.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • can i use two dimensional array in C lang for this? – ajdeguzman Jul 02 '13 at 02:34
  • Sure, you can use a 2D array if you understand what you're going to do with it. You can use a 1D array if you want. Read all your words into the array, sort it, then run through and count up the duplicates. No special data structures necessary. Approximately the same time complexity as using a balanced binary tree, but with a bit more memory overhead. – paddy Jul 02 '13 at 02:40
  • But what is the data type of the array if i'm going to use 1D array? Can i use something like this: `strs[0] = "first string";` ? – ajdeguzman Jul 02 '13 at 03:12
  • It would be an array of `char*`, either of fixed size or dynamic (*ie* `char**`). At this point you need to go and learn the basics of arrays and string-handling in C. This is beyond the scope of your question. – paddy Jul 02 '13 at 03:15
  • I got to look at this example: `#include typedef char * string; int main(void) { string strs[5]; // Make 5 strings int i; strs[0] = "first string"; strs[1] = "second string"; strs[2] = "third string"; strs[3] = "this is the fourth string"; strs[4] = "and finally this is the fifth string"; for(i = 0;i < 5;++i) puts(strs[i]); return 0; }` but i got some error. – ajdeguzman Jul 02 '13 at 03:17
  • Sorry, but I will say it again. **This is beyond the scope of your current question.** Post that as another question. Assigning string literals like that is different from reading and allocating strings from a file. – paddy Jul 02 '13 at 03:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32712/discussion-between-aj-de-guzman-and-paddy) – ajdeguzman Jul 02 '13 at 03:20
  • Regarding your comment above: Does putting code in a comment like that **look** like an effective way to share code?? Do you think that is readable? You should edit your question, or, as @paddy stated, **start a new question**. – abelenky Jul 02 '13 at 14:05