I need help writing a function, we'll call it NumDouble. It takes a string S as an argument and returns the number of times there is a double-letter inside it.... ex: if S=“happy” then the function returns 1.
If S=”balloon” or S=”Zaxxxon” the function returns 2.
If S=”Lollapallooza” the function returns 3… etc
I have the code for the 'main' but need help writing the function. i think i would use a for loop but dont know how to implement it.
1 2 3 4 5 6 7 8 9 10 11
int NumDouble (string S) {
int nd = 0;
int main() {
cout << “Enter a string: “;
string q;
cin >> q;
int x;
x = NumDouble(q);
cout << “Number of doubles in “ << q << “ is “ << x << endl;
system(“pause”);
What you have is a good basis - you're working your way through the string, comparing consecutive characters.
Once you work out your errors (which will be minor, I should think), all you need to add is a running total of the number of doubles found, and to return it from the function.