Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

who can solve this program for me ?

Request :

write a program to count the vowels and alphabets in an input line of charachters, Read the charachters one at a time until you encounter the end of line charachter. then print out :
- the number of occurences of each the vowels (a,e,i,o,u) whether capital or small,
- the total number of alphabets, and
- the integer percentage of each vowel.

note that the program should ignore all non-alphabets.
use the switch statement to solve this problem

example:
for the following input:
A good word is a charity
suggested output format is :

Number of vowels:
a 3 ; e 0 ; i 2 ; o 3 ; u 0

Total number of letters is 19

Integer percentage of each vowel :
a 16%; e 0%, i 11% ; o 16% ; u 0%



my Program:

#include <iostream>
#include <string>
using namespace std;

int main()
{
char dummy;
int count, countA, countE, countI, countO, countU, num, length ;
string S1;
char S;

count = 0 ;
countA = 0 ;
countE = 0 ;
countI = 0 ;
countO = 0 ;
countU = 0 ;
num = 0 ;

cout << endl << endl;
cout << " Enter a sentence : ";
getline(cin,S1);
length = S1.length();

while ( num <= length )
{ S = S1.substr(num,1); // ERROR

while ( S >= 'A' || S <= 'Z' || S >= 'a' || S <= 'z')
{
count++ ;
switch (S)
{
case 'a' :
case 'A' : countA++;
break;
case 'e' :
case 'E' : countE++;
break;
case 'i' :
case 'I' : countI++;
break;
case 'o' :
case 'O' : countO++;
break;
case 'u' :
case 'U' : countU++;
}
}
num++;
}

cout << "\n Number of vowels:\n";
cout << " a : " << countA << endl;
cout << " e : " << countE << endl;
cout << " i : " << countI << endl;
cout << " o : " << countO << endl;

cout << " Total number of letters is : " << count << endl;

cout << " Integer Percentage of each vowel : \n";
cout << " a : " << int(countA*100/count) << "%" << endl;
cout << " e : " << int(countE*100/count) << "%" << endl;
cout << " i : " << int(countI*100/count) << "%" << endl;
cout << " o : " << int(countO*100/count) << "%" << endl;

cin >> dummy;
return 0;
}
[3551 byte] By [PowerBook] at [2007-11-11 8:27:12]
# 1 Re: who can solve this program for me ?
You just can't assign string type to char type like that. I did some changes so that your code should compile and execute but it still has some logical error. Just look into it.

I just started corrected your code as the error came and here is the changed code.

#include <iostream>
#include <string>
using namespace std;

int main()
{
char dummy;
int count, countA, countE, countI, countO, countU, num, length ;
string S1;
string S;

count = 0 ;
countA = 0 ;
countE = 0 ;
countI = 0 ;
countO = 0 ;
countU = 0 ;
num = 0 ;

cout << endl << endl;
cout << " Enter a sentence : ";
getline(cin,S1);
length = S1.length();

while ( num <= length )
{
S = S1.substr(num,1); // ERROR

while ( S >= "A" || S <= "Z" || S >= "a" || S <= "z")
{
count++ ;
switch (atoi(S.c_str()))
{
case 'a' :
case 'A' : countA++;
break;
case 'e' :
case 'E' : countE++;
break;
case 'i' :
case 'I' : countI++;
break;
case 'o' :
case 'O' : countO++;
break;
case 'u' :
case 'U' : countU++;
}
}
num++;
}

cout << "\n Number of vowels:\n";
cout << " a : " << countA << endl;
cout << " e : " << countE << endl;
cout << " i : " << countI << endl;
cout << " o : " << countO << endl;

cout << " Total number of letters is : " << count << endl;

cout << " Integer Percentage of each vowel : \n";
cout << " a : " << int(countA*100/count) << "%" << endl;
cout << " e : " << int(countE*100/count) << "%" << endl;
cout << " i : " << int(countI*100/count) << "%" << endl;
cout << " o : " << int(countO*100/count) << "%" << endl;

cin >> dummy;
return 0;
}
Nokia2280 at 2007-11-11 21:01:21 >
# 2 Re: who can solve this program for me ?
Or you could do something like this

S = reinterpret_cast<char>(S1.substr(num,1).c_str());
Nokia2280 at 2007-11-11 21:02:17 >