DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2006
    Posts
    3

    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;
    }

  2. #2
    Join Date
    Jan 2006
    Posts
    55
    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.

    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;
    }
    Last edited by Nokia2280; 04-11-2006 at 05:19 AM.

  3. #3
    Join Date
    Jan 2006
    Posts
    55
    Or you could do something like this
    Code:
    S = reinterpret_cast<char>(S1.substr(num,1).c_str());

Similar Threads

  1. Help with a program
    By stormswimmer in forum Java
    Replies: 2
    Last Post: 01-02-2006, 03:17 PM
  2. problem in program in c++
    By mheasen in forum Architecture and Design
    Replies: 0
    Last Post: 03-20-2002, 09:24 AM
  3. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 10:06 AM
  4. How can I build copy protection into my program
    By W.Pierce in forum VB Classic
    Replies: 1
    Last Post: 12-11-2001, 08:28 AM
  5. Replies: 0
    Last Post: 12-15-2000, 10:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links