I have a script here that counts even numbers in an int type var.
#include <iostream>
#include <string>
int main()
{
std::cout << "Type a string: " << std::endl;
std::string s;
std::cin >> s;
unsigned int digits = 0, evens = 0;
for ( std::string::size_type i = 0; i < s.size() && s[i] >= '0' && s[i] <= '9'; i++ )
{
++digits;
evens += ( s[i] - '0' ) % 2 == 0;
}
std::cout << "The number has " << digits << " digit(s)." << std::endl;
std::cout << "The number has " << evens << " even digit(s)." << std::endl;
return 0;
}
Im trying to find a way to turn this into a string instead so I can count on how many even numbers or numbers are there in that string?
Type a string:
29 coaches 28
Even: 1
Found Numbers: 1
In python it should be something like:
s = "75,41,14,8,73,45,-16"
evenNumbers = []
for number in s.split(","):
int_num = int(number)
if int_num % 2 == 0 and int_num > 0:
evenNumbers.append(int_num)
print("Even Numbers : \"{}\"".format(evenNumbers))
But I dont know how to do it in C++