### Begginger help, strings.

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.
 ``1234567891011`` ``````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”);``````
¿why does `NumDouble("Zaxxxon")` should return 2?

> i think i would use a for loop but dont know how to implement it.
If you can provide the pseudocode I'll translate it.
i'm a beginner i dont know what a pseudocode is. this is what i have so far im still working on it but im getting some errors.....

#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int NumDouble (string S) {
int nd = 0;
for (int nd=1; nd<s.length( ); nd++);
{
if (S[nd-1]==S[nd]);
}
}
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");
}
Last edited on
I would assign the string to a character array then have a for loop do something like

char x;
char y;

for (n=0;n<10;n++)
{
x= array[n]
y= array [n+1]
if (x==y) numberofdoubles +=1;
}

Or somesuch. Also make a loop that counts how big the array is to begin with (I just used ten as a random number).
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.

What errors are you getting?

Jim
Topic archived. No new replies allowed.