Counting Vowels and Consonants

Dec 14, 2010 at 6:21am
closed account (3vkz3TCk)
Hi! This is my first thread in this forum and since I've got a lot of help by reading other forums, I would like to post a problem of my own.

In this problem, you must type in your full name then the program will count how many vowels and consonants.
Output should look like this.

Enter fullname: Juan De La Cruz
There are 5 vowels and 7 consonants in Juan De La Cruz's name!

Here's the code I made:


#include<iostream>
using namespace std;
int main()

{
char name[30];
int x, vowels = 0, consonants = 0;

cout << "Enter your fullname: ";
cin.getline(name,30);
for (x = 0; x <= name[x]; x++)
{
if (name[x] == 'a')
vowels = vowels + 1;
else if (name[x] == 'e')
vowels = vowels + 1;
else if (name[x] == 'i')
vowels = vowels + 1;
else if (name[x] == 'o')
vowels = vowels + 1;
else if (name[x] == 'u')
vowels = vowels + 1;
else
consonants = consonants + 1;
}
cout << "There are " << vowels << " vowels and " << consonants << " consonants in " << name << "'s name!" << endl;
return 0;
}

THE PROBLEM IS it also counts the spaces in the between the name. :( I need your help! I would really appreciate your effort if you help me. Thanks! :)
Dec 14, 2010 at 7:23am
[code] Your code goes here [/code]
for (x = 0; x <= name[x] /* what? */; x++) The cstring will end with '/0' use that for the loop.

If you encounter anything that is not a letter, just ignore it if( !isalpha(name[x]) ) continue;
Also you could group that conditions looking for vowels if( name[x]=='a' or name[x] == 'e' || /*...*/) or better if( isvowel(name[x]) )
Last edited on Dec 14, 2010 at 7:24am
Dec 14, 2010 at 10:47am
You could create a char array called 'consonantArray' and another called 'vowelArray' and then place a list of all the consonants into the first array and a list of all the vowels into the other. That way you could loop through every character obtained via the input command, and then loop through consonantArray and vowelArray to see if the input character exists in either array.

Then you can be sure that you are not detecting anything other than vowels or consonants.

For example, look at this for some ideas:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdlib.h>	// Contains system()
#include <cstring>
#include <iostream>
using namespace std;

int main(int argc, char** argv){
	
	cout << "\n\n";
	cout << "*******************************************\n";
	cout << "** Start of Input Scanner\n";
	cout << "*******************************************";
	char input[200];
	for(int i = 0; i < 200; i++){
		input[i] = '\0';
	}
	
	printf("\n\nInput some text...");
	cin.getline(input, 200);
	
	printf("\nYou input '%s'\n", input);
	
	char consonants[42] = 	{	'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z',
					'B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'	};
	char vowels[10] = 	{	'a','e','i','o','u',
					'A','E','I','O','U'	};
	
	
	int consonantCount = 0;
	int vowelCount = 0;
	int unidentifiedCount = 0;
	
	bool identifiedCharacter;
	
	printf("\n\nScan started...\n");
	for(int i = 0; i < 200; i++){
		if(input[i]=='\0'){
			i == 200;
		}else{
		
			identifiedCharacter = false;
		
			printf("\n\tScanning for '%c'...", input[i]);
			
			// Check Consonants
			for(int j = 0; j < 42; j++){
				if(input[i] == consonants[j]){
					printf(" match found.");
					consonantCount++;
					identifiedCharacter = true;
					j = 42;
				}
			}
		
			if(!identifiedCharacter){
				// Check vowels
				for(int j = 0; j < 10; j++){
					if(input[i] == vowels[j]){
						printf(" match found.");
						vowelCount++;
						identifiedCharacter = true;
						j = 10;
					}
				}
			}
		
			// Check to see if the character was identified
			if(!identifiedCharacter){
				unidentifiedCount++;
				printf(" no matches found.");
			}
			
		}
	}
	printf("\n\nScan finished.");

	printf("\n\nConstanants detected: %u", consonantCount);
	printf("\nVowels detected: %u", vowelCount);
	printf("\nUnidentified chatacters detected: %u", unidentifiedCount);
		
	
	cout << "\n\n";
	cout << "*******************************************\n";
	cout << "** End of Input Scanner\n";
	cout << "*******************************************";
	cout << "\n\n";

	return 0;
	
}
Dec 16, 2010 at 1:43pm
closed account (3vkz3TCk)
ne555 and aatwo:

Thank you so much!! I really appreciated it!! Our teacher never teaches us anything by the way. He would just give us the output without teaching us new functions and other stuff. Thank you so much!!! :)
Topic archived. No new replies allowed.