How to count words in a file.

I have been looking around for example of how to read a file and count how many words in a sentence, my understanding is counting how many white characters. Is that the approach I should take. I do have other questions but I want to get them done one by one.
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<iostream>
#include<cctype>
#include<cstring>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
#include<string.h>

using namespace std;

//single structure that contains all functions + the line itself
struct Counts
{
  int countChars;
  int countNW;
  int countAlpha;
  int countDigits;
  int countPunctuation;
};

//function names
int countChars(Counts&, char[]);
int countNW(Counts&, char[]);
int countAlpha(Counts&, char[]);
int countDigits(Counts&, char[]);
int countPunctuation(Counts&, char[]);
void printRprt(Counts&);  

int main()
{
  //open input file (C-style)
  FILE *fp;
  fp = fopen("inp1.txt", "r");
  //error message
  if(fp == NULL)
  {
    printf("Could not open input file\n");
    exit(0);
  }

  //declare variables
  Counts counts;
  int i;
  char line[80];
  char delims[] = " ,\t\n"; //delimeters: space, ',', tab, return

  //get lines (max 80) from file
  //a line is a group of characters terminated by a \n
  //Call to the functions
  while(fgets(line, 80, fp))
  {
    printf(line);
    countChars(counts, line);
    countNW(counts, line);
    countAlpha(counts, line);
    countDigits(counts, line);
    countPunctuation(counts, line);
    printRprt(counts);
  }
}

/*
 * Function name: countChars
 * Output: An integer
 * Description: Counts how many characters are on the given line.
*/
int countChars(Counts& counts, char line[])
{
  counts.countChars=0;
  int i=0; 
  while (line[i] !='\0')
  {
   {
   counts.countChars++;
   }
  i++;
  }
}
/*
 * Function name: countNW
 * Output: An integer
 * Description: counts how many characters are not white space.
*/
int countNW(Counts& counts, char line[])
{
  counts.countNW=0;
  char c;
  int i=0;
  while (line[i]!='\0')
  {
    c= line[i];
    if (!isspace(c))
    {
    counts.countNW++;
    }
  i++;
  }
}

/*
 * Function name: countAlpha
 * Output: An integer
 * Description: counts how many characters are alphabetic letters.
*/
int countAlpha(Counts& counts, char line[])
{
  counts.countAlpha=0;
  int i=0;
  while (line[i]!='\0')
  {
    if (isalpha(line[i]))
    {
    counts.countAlpha++;
    }
    i++;
  }
}

/*
 * Function name: countDigits
 * Output: An integer
 * Description: Counts how many characters are numeric digits.
*/
int countDigits(Counts& counts, char line[])
{
  counts.countDigits=0;
  int i;
  i=0;
  while ((line[i]!='\0'))
  {
   if (isdigit(line[i]))
   {
   counts.countDigits++;
   }
  i++;
  }
}

/*
 * Function name: countPunctuation
 * Output: An integer
 * Description: Counts how many characters are punctuation. (non-numeric, non-alphabetical)
*/
int countPunctuation(Counts& counts, char line[])
{
  counts.countPunctuation=0;
  int i=0;
  int cx=0;
  while (line[i]!='\0')
  {
    if (ispunct(line[i]))
    {
    counts.countPunctuation++;
    }
   cx++;
   i++;
  }
}

/*
 * Function name: printReport
 * Output: Results of all the functions.
 * Description: Prints a report of all the functions.
*/
void printRprt(Counts& counts)
{
  cout <<"Total characters: " << counts.countChars <<"\nNon-white space: " << counts.countNW << "\nAlphabetic: " << counts.countAlpha <<"\nDigits: " << counts.countDigits << "\nPunctuation: " << counts.countPunctuation <<endl;
}


Example I found that I sorta modified
1
2
3
4
5
6
7
8
9
10
int countWords(Counts& counts, ){
  counts.countWords = 0;
  char ch;
  while ((ch = fgetc(f)) != EOF)
  {
      if (ch == '\n')
      counts.countWords++;
  }
   return counts.countWords;
}
Last edited on
I'm not sure that's a good method actually.
What if someone types

Hello World ! ! !



Using that example, I'd count white space only if the char before it is a-z or A-Z.

A better way would be to have a dictionary file and if the word is in that file, then it's counted, so that 123a wouldn't be counted.
Last edited on
Topic archived. No new replies allowed.