How would you write a program that counts the number of vowels in a string? |
Answer (c++)
Hi all, I have written a simple and efficient program to calculate number of vowels in a string. check it out
void main()
{
char *str;
char a[]="aeiouAEIOU";
int i,j,count=0;
clrscr();
printf("\nEnter the string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
for(j=0;a[j]!='\0';j++)
if(a[j] == str[i])
{
count++;
break;
}
printf("\nNo of vowels = %d",count);
getch();
}
Regrds, R.Yasotha balan.
Answer (javascript)
Just incase you don't have access to a compiler, here's a html/javascript version.
<script>
b = prompt("Enter the String","");
c=0;
for(i=0;i<b.length;i++) {
if("aeiou".indexOf(b.substr(i,1).toLowerCase())!=-1){ c++;}
}
alert("No of Vowels : "+c);
</script>
Answer (PHP)
<?php
$string = 'This is a test';
$vowels = 'aeiou';
for ($i=0; $i<strlen($string); $i++) {
for ($j = 0; $j<strlen($vowels); $j++) {
if (substr($string, $i, 1) == substr($vowels, $j, 1)) {
$vowelcount++;
}
}
}
print 'There are '.$vowelcount.' vowels in '.$string.'.';
?>
|
|
|
First answer by Lugnut. Last edit by Dav7. Contributor trust: 58 [recommend contributor]. Question popularity: 77 [recommend question]
|
Research your answer: |
Can you answer other questions about programming?
|
|


