For example to traverse a string and count the number of vowels I could use
for (int i=0; (x=inputString[i])!='\0';i++)
if (x=='a' || x=='e' || x=='i' || x=='o' || x=='u') numVowels++;
In functional languages like haskell or erlang we can do lists:member(x,"aeiou")
In some object oriented languages I can do something like ["a","e","i","o","u"].find(x)
What's the cleanest way to do this in C++ without the x==.. || x==.. || ...
inputString.find_first_of("aeiou");
This will give you the first index of any matching element. You can do this in loop to get all matching element:
size_t idx=0;
do{
size_t current = inputString.find_first_of("aeiou",idx);
idx = current;
}
while(current!=string::npos);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With