You are on page 1of 3

#include<iostream>

#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
#include<cstdlib>
void countletters(string str2);
int main()
{
string filename;
cout<<" ** Counting Letters **\n\n"<<"Please enter the filename: ";
// filename is " hello.txt "
cin>>filename;
ifstream file(filename.c_str(),ios::in);
if(!file)
{
cerr<<"File cannot be opened\n";
}
else
{
string str1;
while(file>>str1)
{
countletters(str1);
}
}
}
void countletters(string str2)
{
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;
for(int ctr=0;ctr<str2.length();ctr++)
{
switch(str2[ctr])
{
case 'a':
case 'A':a+=1;break;
case 'b':
case 'B':b+=1;break;
case 'c':
case 'C':c+=1;break;
case 'd':
case 'D':d+=1;break;
case 'e':
case 'E':e+=1;break;
case 'f':
case 'F':f+=1;break;
case 'g':
case 'G':g+=1;break;
case 'h':
case 'H':h+=1;break;
case 'i':
case 'I':i+=1;break;
case 'j':
case 'J':j+=1;break;

case 'k':
case 'K':k+=1;break;
case 'l':
case 'L':l+=1;break;
case 'm':
case 'M':m+=1;break;
case 'n':
case 'N':n+=1;break;
case 'o':
case 'O':o+=1;break;
case 'p':
case 'P':p+=1;break;
case 'q':
case 'Q':q+=1;break;
case 'r':
case 'R':r+=1;break;
case 's':
case 'S':s+=1;break;
case 't':
case 'T':t+=1;break;
case 'u':
case 'U':u+=1;break;
case 'v':
case 'V':v+=1;break;
case 'w':
case 'W':w+=1;break;
case 'x':
case 'X':x+=1;break;
case 'y':
case 'Y':y+=1;break;
case 'z':
case 'Z':z+=1;break;
default: break;
}
}
cout<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n
<<"\n

a:
b:
c:
d:
e:
f:
g:
h:
i:
j:
k:
l:
m:
n:
o:
p:
q:
r:
s:
t:
u:
v:
w:
x:
y:

"<<a
"<<b
"<<c
"<<d
"<<e
"<<f
"<<g
"<<h
"<<i
"<<j
"<<k
"<<l
"<<m
"<<n
"<<o
"<<p
"<<q
"<<r
"<<s
"<<t
"<<u
"<<v
"<<w
"<<x
"<<y

<<"\n z: "<<z
<<endl;
}

You might also like