vowels using find_first_of()

Using the function find_first_of(), I want to make a program that would count the number of vowels of an inputted string. For example, an input of "hello world" would output "3".

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <vector>
#include <cctype>  
using namespace std;  

int main()  {
  string text;
  cout<<"Enter text";
  getline(cin, text);
  //using the find_first_of() function find how many vowels are in the string
  cout<<"Number of vowels ";
}
Hello coder0101,


Not something I do often enough, so I usually start at: http://www.cplusplus.com/reference/string/string/find_first_of/ and adjust the code from there.

You may also want to add a variable to count the number of vowels.

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <cctype>

int main()
{
	std::string text;

	std::cout << "Enter text: ";
	std::getline(std::cin, text);

	size_t novowel {};

	for (size_t strt {}; (strt = text.find_first_of("aeiouAEIOU", strt)) != std::string::npos; ++strt)
		++novowel;

	std::cout << "Number of vowels " << novowel << '\n';
}

Topic archived. No new replies allowed.