c++ program help
Mar 24, 2013 at 4:42pm UTC
can someone help me with a c++ program which reads an input stream from the keyboard that counts the frequency of occurrence of all the letters of the alphabet
Mar 24, 2013 at 5:42pm UTC
Can you post your code so that we can take a look?
Mar 24, 2013 at 7:38pm UTC
Hi,
Just complete the else ifs to account for all letters of the alphabet
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
#include <iostream>
#include <string>
using namespace std;
void count_occurances(const string the_line);
int main(){
char hold_console;
string line;
cout<<"Enter line\n" ;
cout<<">>" ;
getline(cin,line);
count_occurances(line);
cin>>hold_console;
return 0;
}
void count_occurances(const string the_line){
int a_count=0,b_count=0, c_count=0, d_count=0;
for (int i=0;i<the_line.length();i++){
if (isalpha(the_line[i])){
if (the_line[i]=='a' || the_line[i]=='A' )a_count++;
else if (the_line[i]=='b' || the_line[i]=='B' )b_count++;
else if (the_line[i]=='c' || the_line[i]=='C' )c_count++;
else if (the_line[i]=='d' || the_line[i]=='D' )d_count++;
}
}
cout<<"a or A occured: " <<a_count<<" times\n" ;
cout<<"b or B occured: " <<b_count<<" times\n" ;
cout<<"c or C occured: " <<c_count<<" times\n" ;
cout<<"d or D occured: " <<d_count<<" times\n" ;
}
Mar 25, 2013 at 1:50am UTC
@toomanystars-- thank u it worked for me.
i just wanted to ask is it possible to display only the alphabets that are repeated and not all ??
Mar 25, 2013 at 4:58am UTC
Sure,
You just need to add a condition like
1 2
if (a_count>0)cout<<a_count<<endl;
Apr 4, 2013 at 2:30am UTC
it worked thank you...
Topic archived. No new replies allowed.