Vowel count !


I copied this program to count the total vowels..it works just fine but I don't
understand why '++vow_ctr;" is used at the end of the switch case instead with a default statement?! (pls help..urgent)


#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main ()
{ clrscr();
char line[80];
int vow_ctr=0;
cout<<"Enter the line \n ";
gets(line);
for(int i=0;line[i]!='\0';i++)
{ switch(line[i])
{ case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
++vow_ctr;

}
}
cout<<"total no. of vowels \n" <<vow_ctr <<endl;
}
The ++vow_ctr at the end of the switch line is applicable too all of the above cases. Only a break; statement will exit the switch before the next case. Therefore the default case is not really applicable here.

Here is your code with code tags, correct indents, and formatting. This will help you to read the switch properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main ()
{ 
    clrscr();
    char line[80];
    int vow_ctr=0;
    cout<<"Enter the line \n ";
    gets(line);
    for(int i=0;line[i]!='\0';i++)
    { 
        switch(line[i])
        { 
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            ++vow_ctr;
        }
    }
    cout<<"total no. of vowels \n" <<vow_ctr <<endl;
}


This is logically identical to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main ()
{ 
    clrscr();
    char line[80];
    int vow_ctr=0;
    cout<<"Enter the line \n ";
    gets(line);
    for(int i=0;line[i]!='\0';i++)
    { 
        if (line[i] == 'a' ||
	    line[i] == 'e' ||
	    line[i] == 'i' ||
	    line[i] == 'o' ||
	    line[i] == 'u' )
	{
            ++vow_ctr;
        }
    }
    cout<<"total no. of vowels \n" <<vow_ctr <<endl;
}


A default case would be similar to an 'else' statement.
Last edited on
use code tags next time.

++vow_ctr; will be executed if line[i] is a vowel. There is no need for a default statement because nothing should happen in that case.
The vow_ctr variable is incremented whenever it finds a vowel. There is no need for a default statement, as the only other scenario would be the letter being a consonant, a number or a special character, all of which will be ignored, the switch statement will skip, and then repeat for the next letter.
this maybe funny but I have no idea how to use code tags..m new to this site..
But thanks all for the help!...I absolutely understood the working of the switch case now ! :)
To add code tags you just put [code] [/code] around your code.
If you select the code and press the <> button to the left it will add code tags around the selected text.
Last edited on
thanks agaiN!!!!!!!!!!!!!
Topic archived. No new replies allowed.