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.'.';
?>
 

Improve Answer Discuss the question "How would you write a program that counts the number of vowels in a string?" Watch Question

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?

Answers.com > Wiki Answers > Categories > Technology > Computers > Computer Programming > How would you write a program that counts the number of vowels in a string?

Our contributors said this page should be displayed for the questions below. (Where do these come from)
If any of these are not a genuine rephrasing of the question, please help out and edit these alternates.
Write a program in c to count the no of vowels in an entered line?  How would you write a program in C that counts the number of vowels in a string?