Simple word counter

Hi!

I've taken part the text into 1 word per line, but I can't figure out how to printf every word only once and then add (%d) in the end to show how many repetitions of that word there are.

Any help very welcome!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

int main()
{
    char *oneword;
    FILE *fp;
    char line[1000], mark, *p;
    char *filename1="sometextinhere.txt";
    int i, j, words=0, loe[1000], counter=0;
    for(i=0;i<1000;loe[i++]=0);
    fp=fopen(filename1,"r");
    if(!fp){printf("File %s missing!\n", filename1); return 1;}
    printf("File %s statistics: \n", filename1);
    while(!feof(fp)){
        fgets(line, 1000, fp);
        oneword = strtok(line,"\"\n ,.-:;()1234567890");
        while (oneword != NULL){
            for(i=0;i<40;i++){oneword[i]=tolower(oneword[i]);}

            //need help here

            printf ("\t%s (%d)\n",oneword, counter);
            oneword = strtok(NULL, "\"\n ,.-:;()1234567890");
            words++;
        }

    };
    printf("In total: %d words\n", words);
    fclose(fp);
    return 0;
}
Topic archived. No new replies allowed.