Computer Science, asked by vikaries6119, 1 year ago

To count the number of vowels in a text-flowchart?

Answers

Answered by angel77777
1
simple way to do this, whatever the language used, is to step through the characters in the string one by one and compare them with a previously established set of vowels, adding one to a count value every time a character matches one of the vowels. The count value at the end will be the number of vowels. 
Remember to define the set of vowels and the count value and to check for an empty string and for the end of the string. 
Here are some examples of programs that count the number of vowels in a string in different programming languages. C++ Example 1void 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(); 

} C++ Example 2#include 
#include 
int main (void) {char s[100]; 
int i,c=0; 
clrscr(); 
printf("\n Enter a string"); 
scanf("%s",s); for(i=0;s[i]!='\0';i++) 

if (s[i]=='a's[i]=='A's[i]=='e's[i]=='E's[i]=='i's[i]=='I's[i]=='o's[i]=='O's[i]=='u's[i]=='U') { c++; 


printf("\nThe no. of vowels in the given string is=%d",c); 
getch(); 
return 0; 

} Javascript Exampleb = 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); PHP Example$vowels = array('a','e','i','o','u'); 
$string = 'An example string'; 
$length = strlen($string); 
$count = 0; 
for ($i = 0; $i !== $length; $i++;) {if (array_search($length[$i], $vowels)) { $count++; 


echo 'There are (' . $count . ') vowels in the string (' . $string . ').'; 


Another way to do this would be through the use of RegExes/Pattern matching 
Javascript Example: 
str="A string for use of testing"; 
count= str.match(/[aeiou]/gi).length; 
console.log("There are "+count+" vowels"); 
Java Example: 
import java.util.regex.*; 
public static void main(String[] args){ 
String str= "A string for use of testing"; 
Matcher m= Pattern.compile("(?i)[aeiou]").matcher(str); 
int count= 0; 
while (m.find()){ 
count+= m.end()-m.start(); 

System.out.println("There are "+count+" vowels"); 
}
Similar questions