How to use ascii table to count letters ?

The question :
Write a program that will initialize an array of charters to “do be do bo”. The program will call function Statistics() to output a list of all letters that occurred in the array together with the number of times each letter occurs in the array.
The output should look similar to:
The array is: do be do bo
Letter Number of occurrence
d 2
o 3
b 2
e 1
Letter ‘o’ has occurred the most.
The function prototype is:
void Statistics (char a []);



My program:
#include <iostream>
#include<iomanip>
using namespace std;
void Statistics (char a []);
int main()
{

char d[]="do be do bo";
Statistics(d);
return 0;

}

void Statistics (char d [])
{
int freq[26] ={0};
int i=0;
while ( d[i]!='\0')
{
if (d[i]==' ')
{
i++;
continue;
}
else

(freq(d[i]-97))++; //ERROR ONE called object type int is not a function
i++;
}
for (int j=0; j<26; j++)
{
cout<<setw(10)<<"Letter"<<setw(20)
<<"Number Of Ocurance\n";

cout<<setw(10)<<d[j]<<setw(20)<<
freq(d[i]-97); // ERROR TWO called object type int is not a function


}

}


How do i resolve my error ??
Individual elements of an array should be accessed using square brackets, not round ones.
For example, freq[d[i]-97]++;
Yes thank you ! Completely forgot about that.

#include <iostream>
#include<iomanip>
using namespace std;
void Statistics (char a []);
int main()
{

char d[]="do be do bo";
Statistics(d);
return 0;

}

void Statistics (char d [])
{
int freq[26]={0};
int i=0;
while ( d[i]!='\0')
{
if (d[i]==' ')
{
i++;
continue;
}
else

freq[d[i]-97]++;
i++;
}

cout<<"Letter"<<setw(30)<<"Number of ocurance\n";
for (int j=0; j<26; j++)
{
if (freq[d[j]-97]==0)
continue;
else
cout << d[j]<<setw(3)<<freq[d[j]-97]<<endl;


}

}

OUTPUT:
Letter Number of ocurance
2
2
1
3

1606416648
32767
1606416568
32767
1
30
30
!30
"1606416448
#32767
$1606416608
%30
&1606416448
'32767
(5648
)1
*1952112456
+32767
,1606416312
-32767
.1952112456
/32767
230
41952112464
532767
6195456772
7-986691113
81606416592
932767
:2794
;1
>1646292836
?1868832869
@7299616
B195456772
C-986691113
D1606416616
E32767
F-1944721439
G32767
H-1944721439
I32767
L1
N1606416968
O32767
R1606417094
S32767
T1606417116
U32767
V1606417202
W32767
X1606417259
Y32767
Z1606417324
[32767
\1606417337
]32767
^1606417354
_32767
`1606417490
a32767
b1606417543
c32767
d1606417681
e32767
f1606417691
g32767
h1606417734
i32767
j1606417753
k32767
l1606417883
m32767
n1606417899
o32767
p1606418044
q32767
r1606418078
s32767
t1606418210
u32767
x1606416840
y32767
z1606418328
{32767
|1606418347
}32767
~1606418378
32767


Why is it looking like this ??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Statistics( const char s[] )
{
	const size_t N = 26;
	size_t freq[N] = {};

	for ( const char *p = s; *p; ++p )
	{
		if ( *p != ' ' )
		{
			// convert the letter to upper case
			freq[( *p & ~'\x20' ) - 'A']++;
		}
	}

	std::cout << std::setw( 10 ) <<"Letter" << std::setw(20) << "Number Of Ocurance\n";
	for ( size_t i = 0 ; i < N; i++ )
	{
		if ( freq[i] )
		{
			std::cout << std::setw(10) << char( i + 'A' ) << std::setw( 20 ) 
				      << freq[i] << std::endl;
		}
	}
} 
Last edited on
Topic archived. No new replies allowed.