How do you write a program to count the vowels in a string in visual C?

Answer:

VC++


int countVowels(const char* str) {

int i = 0;

int numVowels = 0;
while(str[i] != '\0') {

switch(str[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
++numVowels;
}

++i;
}

return numVowels;

}


VC#


static int countVowels(String str) {

int numVowels = 0;
foreach(char ch in str) {
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
++numVowels;
break;
}
}

return numVowels;

}

First answer by Moobler. Last edit by Moobler. Contributor trust: 354 [recommend contributor recommended]. Question popularity: 0 [recommend question].