answersLogoWhite

0


Best Answer

#include #include void main() { char string[50]; int vowel=0,consonant=0; cout<<"Enter the string"; cin.getline(string,50); for(int i=0;string[i]!='\0';i++) { switch (string[i]) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U':vowel++; continue; } if (string[i]!=' ') consonant++; } cout<<"No of vowels="<<<"\nNo of consonants="<

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

A 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");

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include<stdio.h>

#include<conio.h>

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("\n the nos. of vowels in the given string is=%d",c);

getch();

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

//testing code

//note that a space character should be neither a vowel, nor a consonant

string s = "This is a test";

Console.WriteLine("Source string: {0}", s);

Console.WriteLine("# of Vowels: {0}",

GetCount(s, "aeiou"));

Console.WriteLine("# of Consonants: {0}",

GetCount(s, "bcdfghjklmnpqrstvwxyz"));

// helpers

// get number of characters in source that are in charactersToMatch

public int GetCount(string source, string charactersToMatch)

{

char[] target = charactersToMatch.ToCharArray();

int count = 0;

foreach (char c in source.ToLower()) {

if (IsMatch(c, target)) count++;

}

return count;

}

//returns true if c is one of patterns, false otherwise

public bool IsMatch(char c, char[] patterns)

{

foreach (char p in patterns) {

if (c == p) return true;

}

return false;

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#include<iostream.h>

#include<conio.h>

main()

{

char str[10];

int v=0;

cout<<"Please enter a string: ";

cin>>str;

for(int i=0;str[i]!='\0';i++)

{

if(str[i]=='a'str[i]=='A'str[i]=='e'str[i]=='E'str[i]=='i'str[i]=='I'str[i]=='o'str[i]=='O'str[i]=='u'str[i]=='U')

v++;

}

cout<<" Number of Vowels are there.= "<<v;

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#include

int str_vowels(char* s)

{

int numbers = 0;

int n = 0;

while(s[n]!='\0')

{

if(s[n]=='a's[n]=='e's[n]=='i's[n]=='o's[n]=='u')

{

numbers++;

}

n++;

}

return numbers;

}

int main()

{

char s[1024];

printf("Enter a string:");

scanf("%s", s);

printf("The numbers of vowels present in the string is %d\n", str_vowels(s));

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

For PHP:

<?php

$string="umbrella";

echo "There are ".preg_match_all('/[aeiou]/',$string,$matches)." vowels in string "."<strong>".$string."</strong>";

?>

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

To find vowels present in string, you must first scan the string for values. Scan each letter on string (as characters) and analyze if they are vowels or not. You can use for loop (since loops are best used with arrays) and conditional statements to scan and analyze each letter. If and Else statements can be handy. But you can also use switch. Also, you will need to use <string.h> in extracting and analyzing words.

char strword[7] = "create"

length = strlen(strword);

for (ctr = 0; ctr < length; ctr++) {

if (strword[ctr] 'u') {

printf ("%c <-- This letter is a vowel\n", strword[ctr]);

}

}

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

In most cases you use C++, but for your general question, you use pseudocode.

This answer is:
User Avatar

User Avatar

Wiki User

16y ago

Yes, you can, but you have to put codes of all vowels in your program.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you count the vowels in a string?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

C plus plus programme to count non-vowels in a file of test?

#include&lt;iostream&gt; #include&lt;fstream&gt; #include&lt;string&gt; int main() { size_t count=0; std::string vowels ("aeiouAEIOU"); std::ifstream ifs; ifs.open ("test.txt", std::ios::in); if (ifs.bad()) { ifs.close(); std::cerr &lt;&lt; "Invalid input file.\n" &lt;&lt; std::endl; return; } while (!ifs.eof()) { char c = ifs.get(); if ((c&gt;='a' &amp;&amp; c&lt;='z') (c&gt;='A' &amp;&amp; c&lt;='Z')) if (vowels.find (c) != vowels.npos) ++count; } ifs.close(); std::cout &lt;&lt; "The file has " &lt;&lt; count &lt;&lt; " non-vowels.\n" &lt;&lt; std::endl; }


Write a program that stores vowels in an array When you program is given a character it should indicate whether the character is vowel or not?

That's easy to do!This script will get the POST data from an HTML form and check if it is a vowel.


Write a program to accept a string from keyboard and count and display the numbers of vowels present in the string by using function?

You need to scan through the string and keep track of the vowelsoccurring. Here is a sample program:#include#includeint countVowels(char[] s){int count = 0, i;for( i=0; char[i] != '\0'; i++){switch(char[i]){case 'a':case 'e':case 'i':case 'u':case 'o': count++;break;}}return count;}int main(){char str[256];printf("Enter the string:\t");scanf("%s", str);printf("The number of vowels in the string are :%d\n", countVowels(str));return 0;}


How do you write a program to check the string of a given grammar?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; #include&lt;string.h&gt; void main() { char string[50]; int flag,count=o; clrscr(); printf("The grammar is: S-&gt;aS, S-&gt;Sb, S-&gt;ab\n"); printf("Enter the string to be checked:\n"); gets(string); if(string[0]=='a') { flag=0; for(count=1;string[count-1]!='\0';count++) { if(string[count=='b']) { flag=1; continue; } else if((flag==1)&amp;&amp;(string[count]=='a')) { printf("The string does not belong to the specified grammar"); break; } else if(string[count=='a']) continue; else if(flag==1)&amp;&amp;(string[count]='\0')) { printf("The string accepted"); break; } else { printf("String not accepted"); } getch():


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

flow chart

Related questions

How do you count the vowels in a string using PHP?

build an array of vowels then do a foreach on the array and then explode the string on the array value and the answer is -1 of the result


Code of find vowels in string in php?

$vowel_arr=array('a','e','i','o','u'); $string="This is my sentence"; $len=strlen($string); $vowel_cnt=0; for($i=0;$i&lt;$len;$i++) { if(in_array($string[$i],$vowel_arr)) $vowel_cnt++; else continue; } echo "Total Vowel count is: ".$vowel_cnt;


C plus plus programme to count non-vowels in a file of test?

#include&lt;iostream&gt; #include&lt;fstream&gt; #include&lt;string&gt; int main() { size_t count=0; std::string vowels ("aeiouAEIOU"); std::ifstream ifs; ifs.open ("test.txt", std::ios::in); if (ifs.bad()) { ifs.close(); std::cerr &lt;&lt; "Invalid input file.\n" &lt;&lt; std::endl; return; } while (!ifs.eof()) { char c = ifs.get(); if ((c&gt;='a' &amp;&amp; c&lt;='z') (c&gt;='A' &amp;&amp; c&lt;='Z')) if (vowels.find (c) != vowels.npos) ++count; } ifs.close(); std::cout &lt;&lt; "The file has " &lt;&lt; count &lt;&lt; " non-vowels.\n" &lt;&lt; std::endl; }


Write a program to accept a string from keyboard and count and display the numbers of vowels present in the string by using function?

You need to scan through the string and keep track of the vowelsoccurring. Here is a sample program:#include#includeint countVowels(char[] s){int count = 0, i;for( i=0; char[i] != '\0'; i++){switch(char[i]){case 'a':case 'e':case 'i':case 'u':case 'o': count++;break;}}return count;}int main(){char str[256];printf("Enter the string:\t");scanf("%s", str);printf("The number of vowels in the string are :%d\n", countVowels(str));return 0;}


Write a program that stores vowels in an array When you program is given a character it should indicate whether the character is vowel or not?

That's easy to do!This script will get the POST data from an HTML form and check if it is a vowel.


How many vowels are in the word hypothesis?

There are 3 vowels in "hypothesis" (4 if you count the 'y').


How do you count no of vowels and print also in java?

suppose we have a string String s = "hello how are you..."; char arr[] = s.toCharArray(); for(i = 0; i &lt; arr.length; i++) { if(arr[i] 'u') System.out.println(arr[i]); } that is it .I hope it works.If there is a better process let me know at :- "hello2.abhishek.pal@gmail.com"...


How do you write c program to accept a string from the console and count number of vowels constants digits tabs and blank spaces in a string?

Read the characters one at a time, and write an "if" for each of the cases. In each case, if the condition is fulfilled, increment the corresponding counter variable.


How do you write a program to check the string of a given grammar?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; #include&lt;string.h&gt; void main() { char string[50]; int flag,count=o; clrscr(); printf("The grammar is: S-&gt;aS, S-&gt;Sb, S-&gt;ab\n"); printf("Enter the string to be checked:\n"); gets(string); if(string[0]=='a') { flag=0; for(count=1;string[count-1]!='\0';count++) { if(string[count=='b']) { flag=1; continue; } else if((flag==1)&amp;&amp;(string[count]=='a')) { printf("The string does not belong to the specified grammar"); break; } else if(string[count=='a']) continue; else if(flag==1)&amp;&amp;(string[count]='\0')) { printf("The string accepted"); break; } else { printf("String not accepted"); } getch():


How would you implement a substr function that extracts a sub string from a given string?

substr(string, position [, count]) It extract substring starting from start and going for count characters. If count is not specified, the string is clipped from the start till the end


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

flow chart


What is the collective noun for vowels?

There is no specific collective noun for the noun vowels, in which case a noun suitable for the situation can be use, for example a group of vowels, a string of vowels, a bunch of vowels, etc.