char arrray, how count the numbers of letters in a paragraph ?

Hi guys,
the below program calculates the amount of capital and lower case letters on a paragraph saved in my flash drive. The program works fine ,but I would like to change the program so I dont use any global counters and instead just use an array. Can someone please point me the right direction ? Thanks to everyone in advance.

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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
 
#include <iostream>
using namespace std;
#include <fstream>
#include <iomanip>
#include <ctype.h> 
void upper_case(char letter[]);
void writevalues();
int A = 0, B = 0, C = 0, D = 0, E = 0, F = 0, G = 0, H = 0, I = 0, J = 0, K = 0, L = 0, M = 0, N = 0, O = 0, P = 0, Q = 0, R = 0, S = 0, T = 0, U = 0, V = 0, W = 0, X = 0, Y = 0, Z = 0;
int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0, k = 0, l = 0, m = 0, n = 0, o = 0, p = 0, q = 0, r = 0, s = 0, t = 0, u = 0, v = 0, w = 0, x = 0, y = 0, z = 0;
int main()
{
	char letter[1000];


	ifstream infile("I:\\TEXT DAT.txt");
	if (!infile)
	{
		std::cout << "Cannot open input file. Program terminates!\n";
		return 1;
	}

	do {
		infile >> letter;
		//call fucntion to calculate upper case letters
		upper_case(letter);
		//call function to write values to output file
		writevalues();


	} while (!infile.eof());

	return 0;
}


void upper_case(char letter[])
{

	for (int z = 0; letter[z] != '\0'; z++)
	{

		if (letter[z] == 'A')
		{

			A++;
		}
		else if (letter[z] == 'a')
		{
			a++;

		}
		else if (letter[z] == 'b')
		{
			b++;
		}
		else if (letter[z] == 'B')
		{
			B++;
		}

		else if (letter[z] == 'C')
		{
			C++;
		}
		else if (letter[z] == 'c')
		{
			c++;
		}

		else if (letter[z] == 'D')
		{
			D++;
		}
		else if (letter[z] == 'd')
		{
			d++;
		}

		else if (letter[z] == 'E')
		{
			E++;
		}
		else if (letter[z] == 'e')
		{
			e++;
		}

		else if (letter[z] == 'F')
		{
			F++;
		}
		else if (letter[z] == 'f')
		{
			f++;
		}

		else if (letter[z] == 'G')
		{
			G++;
		}
		else if (letter[z] == 'g')
		{
			g++;
		}

		else if (letter[z] == 'H')
		{
			H++;
		}
		else if (letter[z] == 'h')
		{
			h++;
		}

		else if (letter[z] == 'I')
		{
			I++;
		}
		else if (letter[z] == 'i')  
		{
			i++;
		}

		else if (letter[z] == 'J')
		{
			J++;
		}
		else if (letter[z] == 'j')
		{
			j++;
		}

		else if (letter[z] == 'K')
		{
			K++;
		}
		else if (letter[z] == 'k')
		{
			k++;
		}

		else if (letter[z] == 'L')
		{
			L++;
		}
		else if (letter[z] == 'l')
		{
			l++;
		}

		else if (letter[z] == 'M')
		{
			m++;
		}
		else if (letter[z] == 'm')
		{

			m++;
		}

		else if (letter[z] == 'N')
		{
			N++;
		}
		else if (letter[z] == 'n')
		{
			n++;
		}

		else if (letter[z] == 'O')
		{
			O++;
		}
		else if (letter[z] == 'o')
		{
			o++;
		}

		else if (letter[z] == 'P')
		{
			P++;
		}
		else if (letter[z] == 'p')
		{
			p++;
		}

		else if (letter[z] == 'Q')
		{
			Q++;
		}
		else if (letter[z] == 'q')
		{
			q++;
		}

		else if (letter[z] == 'R')
		{
			R++;
		}
		else if (letter[z] == 'r')
		{
			r++;
		}

		else if (letter[z] == 'S')
		{
			S++;
		}
		else if (letter[z] == 's')
		{
			s++;
		}

		else if (letter[z] == 'T')
		{
			T++;
		}
		else if (letter[z] == 't')
		{
			t++;
		}

		else if (letter[z] == 'U')
		{
			U++;
		}
		else if (letter[z] == 'u')
		{
			u++;
		}

		else if (letter[z] == 'V')
		{
			V++;
		}
		else if (letter[z] == 'v')
		{
			v++;
		}

		else if (letter[z] == 'W')
		{
			W++;
		}
		else if (letter[z] == 'w')
		{
			w++;
		}

		else if (letter[z] == 'X')
		{
			X++;
		}
		else if (letter[z] == 'x')
		{
			x++;
		}

		else if (letter[z] == 'Y')
		{
			Y++;
		}
		else if (letter[z] == 'y')
		{
			y++;
		}

		else if (letter[z] == 'Z')
		{
			Z++;
		}
		else if (letter[z] == 'z')
		{
			z++;
		}

	}

}

void writevalues()
{
	//Open lab7g_output.txt
	ofstream outfile("I:\\lab7g_output.txt");
	outfile << left;
	if (!outfile)
	{
		std::cout << "Cannot open input file. Program terminates!\n";

	}

	
	outfile << fixed << showpoint << setprecision(2);

	outfile << "A=" << a + A << endl;
	outfile << "B=" << B + b << endl;
	outfile << "C=" << C + c << endl;
	outfile << "D=" << D + d << endl;
	outfile << "E=" << E + e << endl;
	outfile << "F=" << F + f << endl;
	outfile << "G=" << G + g << endl;
	outfile << "H=" << H + h << endl;
	outfile << "I=" << I + i << endl;
	outfile << "J=" << J + j << endl;
	outfile << "k=" << K + k << endl;
	outfile << "L=" << L + l << endl;
	outfile << "M=" << M + m << endl;
	outfile << "N=" << N + n << endl;
	outfile << "O=" << O + o << endl;
	outfile << "P=" << P + p << endl;
	outfile << "Q=" << Q + q << endl;
	outfile << "R=" << R + r << endl;
	outfile << "S=" << S + s << endl;
	outfile << "T=" << T + t << endl;
	outfile << "U=" << U + u << endl;
	outfile << "V=" << V + v << endl;
	outfile << "W=" << W + w << endl;
	outfile << "X=" << X + x << endl;
	outfile << "Y=" << Y + y << endl;
	outfile << "Z=" << Z + z << endl;

}
Last edited on
I'd recommend you have a function to read the data in your file, just to make main() cleaner. Also, you can convert all the characters in your file to uppercase/lowercase, using toupper() or tolower(), respectively. That way, you don't need to worry about case.

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
const int NUM_LETTERS{ 26 };    // global variable

// 'A'/'a' at index 0, 'B'/'b' at index 1, 'C'/'c' at index 2, etc...
int letter_count[NUM_LETTERS]{};    // in main()

string read_file( const string& filename )
{
    // read entire file into a string
}

void write_file( const string& filename, const string& data )

void count_letters( const string& data, int lttr_cnt[NUM_LETTERS] )
{
    for( char c : data ) {
        c = tolower( c );

        if( islower( c ) ) {
            // ASCII code of upper case letters
            // 'a' == 97 to 'z' == 122
            // e.g. if c == 'd'
            // 'd' - 'a' == 100 - 97
            //           == 3
            // which happens to be the index
            // of lttr_cnt which corresponds
            // to the count of 'd'
            lttr_cnt[ c - 'a' ]++;
        }
        else
            // punctuation, numbers, etc...
    }
}
Last edited on
thanks ,

integralfx (591) is there any way to do this with no gloabal variables just by passing the array ?
Yeah, that's possible as well.
void count_letters( const string& data, int lttr_cnt[], int num_lttrs )
closed account (48T7M4Gy)
You can extend this by studying the ASCII table and apply filters and aggregation as you need:

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
#include <iostream>
#include <fstream>

int main ()
{
    char character;
    int array[255] = {0};
    
    std::ifstream myfile ("text_file.txt");
    if (myfile.is_open())
    {
        while ( myfile >> character )
        {
            array[character]++;
        }
        myfile.close();
        
        for(int i = 'a'; i <= 'z'; i++)
            std::cout << (char)i << ": " << array[i] << '\n';
        
        for(int i = 'A'; i <= 'Z'; i++)
                std::cout << (char)i << ": " << array[i] << '\n';
    }
    
    else std::cout << "Unable to open file";
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.