input of a string

Discussion in 'C++' started by Prof.Stanley, May 27, 2006.

  1. Prof.Stanley

    Prof.Stanley Guest

    Hello all, please i am trying to write a program that recieves a string
    from user and counts double vowels.I have a problem particularly in the
    way to input the string,below is my code please will somebody help me
    correct any syntax or semantic error or any proposition.

    Thanks.




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

    void scanner(char s[]);
    bool isVowel(char);
    int length(char s[]);

    int main(){

    //string sentence;
    const int max=4096;
    char sentence[max];
    cout << "Enter $ to terminate!" <<endl;
    do{
    cout << "\nEnter the input string: ";
    getline(sentence,max);
    scanner(sentence);
    }while(sentence != "$");
    return 0;
    }

    void scanner(char s[]){
    int stringSize = length(char s[]);
    char current,next;
    int count = 0;
    int aa,ae,ai,ao,au,ea,ee,ei,eo,eu,ia,ie,ii,io,iu,oa,oe,oi,oo,ou,
    ua,ue,ui,uo,uu;
    aa = ae = ai = ao = au = 0;
    ea = ee = ei = eo = eu = 0;
    ia = ie = ii = io = iu = 0;
    oa = oe = oi = oo = ou = 0;
    ua = ue = ui = uo = uu = 0;
    while(count < stringSize){
    current = s[count];
    if( (count + 1) == stringSize)
    break;
    next = s[count + 1];
    if(isVowel(current) && isVowel(next)){
    //a
    current = tolower(current);
    next = tolower(next);
    if( current == 'a' && next == 'a')
    aa++;
    else if( current == 'a' && next == 'e')
    ae++;
    else if( current == 'a' && next == 'i')
    ai++;
    else if( current == 'a' && next == 'o')
    ao++;
    else if( current == 'a' && next == 'u')
    au++;
    //e
    else if( current == 'e' && next == 'a')
    ea++;
    else if( current == 'e' && next == 'e')
    ee++;
    else if( current == 'e' && next == 'i')
    ei++;
    else if( current == 'e' && next == 'o')
    eo++;
    else if( current == 'e' && next == 'u')
    eu++;
    //i
    else if( current == 'i' && next == 'a')
    ia++;
    else if( current == 'i' && next == 'e')
    ie++;
    else if( current == 'i' && next == 'i')
    ii++;
    else if( current == 'i' && next == 'o')
    io++;
    else if( current == 'i' && next == 'u')
    iu++;
    //o
    else if( current == 'o' && next == 'a')
    oa++;
    else if( current == 'o' && next == 'e')
    oe++;
    else if( current == 'o' && next == 'i')
    oi++;
    else if( current == 'o' && next == 'o')
    oo++;
    else if( current == 'o' && next == 'u')
    ou++;
    //u
    else if( current == 'u' && next == 'a')
    ua++;
    else if( current == 'u' && next == 'e')
    ue++;
    else if( current == 'u' && next == 'i')
    ui++;
    else if( current == 'u' && next == 'o')
    uo++;
    else /*( current == 'u' && next == 'u')*/
    uu++;

    count++;
    }
    else{
    count++;
    continue;
    }
    }
    int totalVowels = aa + ae + ai + ao + au +
    ea + ee + ei + eo + eu +
    ia + ie + ii + io + iu +
    oa + oe + oi + oo + ou +
    ua + ue + ui + uo + uu ;
    if(totalVowels == 0){
    cout << "No double vowels!" <<endl;
    }
    if(aa > 0){
    cout << aa <<"x "<<"aa";
    totalVowels -= aa;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ae > 0){
    cout << ae <<"x "<<"ae";
    totalVowels -= ae;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ai > 0){
    cout << ai <<"x "<<"ai";
    totalVowels -= ai;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ao > 0){
    cout << ao <<"x "<<"ao";
    totalVowels -= ao;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(au > 0){
    cout << au <<"x "<<"au";
    totalVowels -= au;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ea > 0){
    cout << ea <<"x "<<"ea";
    totalVowels -= ea;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ee > 0){
    cout << ee <<"x "<<"ee";
    totalVowels -= ee;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ei > 0){
    cout << ei <<"x "<<"ei";
    totalVowels -= ei;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(eo > 0){
    cout << eo <<"x "<<"eo";
    totalVowels -= eo;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(eu > 0){
    cout << eu <<"x "<<"eu";
    totalVowels -= eu;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ia > 0){
    cout << ia <<"x "<<"ia";
    totalVowels -= ia;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ie > 0){
    cout << ie <<"x "<<"ie";
    totalVowels -= ie;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ii > 0){
    cout << ii <<"x "<<"ii";
    totalVowels -= ii;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(io > 0){
    cout << io <<"x "<<"io";
    totalVowels -= io;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(iu > 0){
    cout << iu <<"x "<<"iu";
    totalVowels -= iu;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(oa > 0){
    cout << oa <<"x "<<"oa";
    totalVowels -= oa;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(oe > 0){
    cout << oe <<"x "<<"oe";
    totalVowels -= oe;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(oi > 0){
    cout << oi <<"x "<<"oi";
    totalVowels -= oi;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(oo > 0){
    cout << oo <<"x "<<"oo";
    totalVowels -= oo;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ou > 0){
    cout << ou <<"x "<<"ou";
    totalVowels -= ou;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ua > 0){
    cout << ua <<"x "<<"ua";
    totalVowels -= ua;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ue > 0){
    cout << ue <<"x "<<"ue";
    totalVowels -= ue;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(ui > 0){
    cout << ui <<"x "<<"ui";
    totalVowels -= ui;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(uo > 0){
    cout << uo <<"x "<<"uo";
    totalVowels -= uo;
    if(totalVowels == 0)
    cout <<".";
    else cout <<", ";
    }
    if(uu > 0){
    cout << uu <<"x "<<"uu"<<".";
    totalVowels -=uu;
    }
    }

    bool isVowel(char c){
    return (c == 'a' || c == 'A' || c == 'e' ||
    c == 'E' || c == 'i' || c == 'I' || c == 'o' ||
    c == 'O' || c == 'u' || c == 'U') ;
    }

    int length(char s[]){

    int i=0;
    while(s='\0')
    i=i+1;

    return i;



    }
     
    Prof.Stanley, May 27, 2006
    #1
    1. Advertising

  2. Prof.Stanley

    Luke Meyers Guest

    Prof.Stanley wrote:
    > Hello all, please i am trying to write a program that recieves a string
    > from user and counts double vowels.I have a problem particularly in the
    > way to input the string,below is my code please will somebody help me
    > correct any syntax or semantic error or any proposition.


    Hi there. If you have any syntax errors, your compiler should catch
    them for you. The way to catch semantic errors, apart from the initial
    effort of simply inspecting your source code as you work with it, is to
    write test code -- that is, little functions which make use of the
    functionality you've implemented, and check that it does the right
    thing. I highly (HIGHLY) recommend the "cppunit" framework, which is
    very simple to use and freely available.

    Now, I'll venture to make a few stylistic comments about your code. In
    general, it seems you've taken a very repetitive, brute-force approach
    to solving this problem. Because there are 5 vowels, you individually
    handle 5*5==25 cases. It would be far better to think a little longer
    before diving in, to see if there's some way to exploit the similarity
    between all those cases. There's no reason you can't write one much
    smaller chunk of code that handles all cases.

    Consider the consequences of the brute-force approach. What if rather
    than being interested in solving the problem for 5 vowels, you were
    looking for duplicates drawn from a set of 100 characters? If you took
    the same approach, you'd have to hand-write 100*100==10,000 cases!
    Similarly, what if you wanted to find sequences of more than just two
    vowels? To find three in a row, you'd be writing 5*5*5==125 cases.
    For four, you'd write 625, and so on.

    The point is, mindless repetition is the computer's job, not yours.
    Design your program in such a way that you take advantage of what the
    computer's good at. As you go on in your programming career, you will
    learn to appreciate more and more the benefit of obsessively avoiding
    duplication and striving for generality. The rewards are innumerable.

    > void scanner(char s[]);
    > bool isVowel(char);
    > int length(char s[]);


    You should be using std::string, not char arrays.

    > int main(){
    > //string sentence;


    I see that you tried it with strings -- why did you give up?

    > const int max=4096;
    > char sentence[max];


    What happens if the user wants to input a string longer than 4096
    characters? There's no reason for such a restriction. That's part of
    why you should use strings rather than character arrays -- they grow
    dynamically to be as long as you need.

    > cout << "Enter $ to terminate!" <<endl;
    > do{
    > cout << "\nEnter the input string: ";
    > getline(sentence,max);


    Use cin instead of getline(), e.g.
    cin >> sentence;

    > scanner(sentence);


    This function has too many responsibilities. I recommend putting your
    logic for finding double vowels in a different function than the logic
    for displaying your results. If they need to share a lot of state
    (e.g. the results of the search), you could make them member functions
    of the same class. It's a good idea to use verbs (or verb phrases) to
    name your functions -- a noun like "scanner" isn't as clear about what
    the function is responsible for. If its job is to "scan," then why is
    it also doing output? Do some reading about the "single responsibility
    principle" -- you won't regret it.

    > void scanner(char s[]){
    > int stringSize = length(char s[]);
    > char current,next;
    > int count = 0;
    > int aa,ae,ai,ao,au,ea,ee,ei,eo,eu,ia,ie,ii,io,iu,oa,oe,oi,oo,ou,
    > ua,ue,ui,uo,uu;
    > aa = ae = ai = ao = au = 0;
    > ea = ee = ei = eo = eu = 0;
    > ia = ie = ii = io = iu = 0;
    > oa = oe = oi = oo = ou = 0;
    > ua = ue = ui = uo = uu = 0;


    Here's where the brute force begins. How many variables would you have
    to type if you increased the problem size? A simple change in the
    problem description should usually mean a simple change in the
    implementation. The code here lacks that useful property.

    You've obviously got a collection of similar things here that you're
    interested in storing. Why not use a container, such as std::vector or
    std::map, to keep them all in one place? Then, you could access them
    all in a more uniform way, for example in the body of a loop.

    > bool isVowel(char c){
    > return (c == 'a' || c == 'A' || c == 'e' ||
    > c == 'E' || c == 'i' || c == 'I' || c == 'o' ||
    > c == 'O' || c == 'u' || c == 'U') ;
    > }


    bool isVowel(char c)
    {
    static std::string const vowels("aeiouAEIOU");
    return (vowels.find(c) != std::string::npos);
    }

    > int length(char s[]){

    \> int i=0;
    > while(s='\0')
    > i=i+1;
    > return i;
    > }


    Learn to use what's already available in the standard library. If you
    were using strings, you could just use string::size(). Even for
    character arrays, you can use strlen.

    Luke
     
    Luke Meyers, May 27, 2006
    #2
    1. Advertising

  3. Prof.Stanley

    Jaspreet Guest

    Luke Meyers wrote:
    > Prof.Stanley wrote:
    > > Hello all, please i am trying to write a program that recieves a string
    > > from user and counts double vowels.I have a problem particularly in the
    > > way to input the string,below is my code please will somebody help me
    > > correct any syntax or semantic error or any proposition.

    >

    <snip>
    > > int main(){
    > > //string sentence;

    >
    > I see that you tried it with strings -- why did you give up?
    >
    > > const int max=4096;
    > > char sentence[max];

    >
    > What happens if the user wants to input a string longer than 4096
    > characters? There's no reason for such a restriction. That's part of
    > why you should use strings rather than character arrays -- they grow
    > dynamically to be as long as you need.
    >
    > > cout << "Enter $ to terminate!" <<endl;
    > > do{
    > > cout << "\nEnter the input string: ";
    > > getline(sentence,max);

    >
    > Use cin instead of getline(), e.g.
    > cin >> sentence;
    >


    I could not understand as to why are you recommending the use of cin ?
    Will it take care of cases if the string has whitespaces in between ?

    <snip>
     
    Jaspreet, May 28, 2006
    #3
  4. Prof.Stanley

    Luke Meyers Guest

    Jaspreet wrote:
    > Luke Meyers wrote:
    > > Use cin instead of getline(), e.g.
    > > cin >> sentence;
    > >

    >
    > I could not understand as to why are you recommending the use of cin ?
    > Will it take care of cases if the string has whitespaces in between ?


    Ya got me. I was focusing on the broad strokes (hey, you saw the
    code...) and flubbed this part. I don't actually use cin much, so I
    had to check just now. getline is the thing to use, but the overload
    for cin and std::string is preferable to the char* way.

    std::string s;
    std::getline(std::cin, s);

    Luke
     
    Luke Meyers, May 28, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    12
    Views:
    1,690
  2. chongo
    Replies:
    2
    Views:
    8,213
    Edward
    Jul 8, 2004
  3. ashutosh
    Replies:
    3
    Views:
    9,035
    Andrew Thompson
    Jun 16, 2005
  4. Tung Chau
    Replies:
    1
    Views:
    476
    SM Ryan
    Aug 6, 2004
  5. Tung Chau
    Replies:
    0
    Views:
    386
    Tung Chau
    Aug 6, 2004
Loading...

Share This Page