Write a full program (starting from #include) ) that is supposed to get an input sentence from the user and write a function that counts the number of character doubles in a phrase entered by the user. The output should look like the example below.
Enter sentence: Hobbits love cookies!! There are 3 character doubles.
You do not have to worry about detecting upper vs. lower-case letters, nor do you have to handle the case when a character occurs three times or more in a row.
Can anyone help me please ?
We only help if you have a specific issue with the assignment given to you.
write a function that counts the number of character doubles in a phrase
That is the first thing you should do. Forget about if it is entered by the user.
Code:
#include <iostream>
int NumDoubleLetters(const char* phrase)
{
// your code goes here
}
using namespace std;
int main()
{
// Test it
int dCount = NumDoubleLetters("Hobbits love cookies!!");
cout << "The number of doubles is " << dCount << "\n";
// Another test
dCount = NumDoubleLetters("abc12312");
cout << "The number of doubles is " << dCount;
}
This is basically the entire assignment, boiled down to the bare minimum. Without that function, the rest of your assignment isn't going to get you anywhere.
So what problem are you having writing this function?
lol i was waiting e replay " We wont do your homework for you " BTW .
Can you help me only with this part : I have this code which find a letter a and A . Now how to write like this but to detect the double letters :
Put away the programming for now.
Write down exactly the steps, even if it's in English, to traverse a string, searching for double letters. Otherwise, you're trying to conjure up magic by writing code, and programming doesn't work that way. Always plan what you're going to do before writing a single line of code.
You encounter a letter, is it a double, yes, no, etc. That is what you're supposed to plan out on paper before you write any function.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 21st, 2012 at 11:32 AM.
I'll add that you're not going to be looking for specific character like A, B C, etc. All you care about is whether the adjacent character is the same or not, not what that character actually is.
lol i was waiting e replay " We wont do your homework for you " BTW .
Can you help me only with this part : I have this code which find a letter a and A . Now how to write like this but to detect the double letters :
n=strlen(a);
for ( i=0; i<20; i++)
if ((a[i]=='a')||(a[i]=='A'))
count ++;
I assume a is the string. Why are you getting its length but hardcoding the upper bound on your loop?
for (int i = 0; i<n; i++)
if (tolower(text[i]) == tolower(text[i+1]))
numDoubles++;
if (numDoubles==1)
cout << "There is "<< numDoubles << " character double."<<endl;
else
cout << "There are "<< numDoubles << " character doubles."<<endl;
cin.get();cin.get();
return 0;
}
and i get this error
Error 1 error C3861: 'stlen': identifier not found
SO i write this combining some other exercise i have done before :
1) Use code tags when posting code. The code you posted is almost unreadable.
2) How does that program work if there just happens to be three characters in a row such as "aaabb"?
3) Why are you repeating this:
Code:
if (numDoubles==1)
cout << "There is "<< numDoubles << " character double."<<endl;
else
cout << "There are "<< numDoubles << " character doubles."<<endl;
Why do you have a special case if numDoubles == 1?
1) Use code tags when posting code. The code you posted is almost unreadable.
2) How does that program work if there just happens to be three characters in a row such as "aaabb"?
3) Why are you repeating this:
Code:
if (numDoubles==1)
cout << "There is "<< numDoubles << " character double."<<endl;
else
cout << "There are "<< numDoubles << " character doubles."<<endl;
Why do you have a special case if numDoubles == 1?
i did some modifications :
#include <iostream>
using namespace std;
int main ()
{
int numDoubles = 0;
char text[30];
cout<<"Enter sentence: ";
cin.getline(text,30);
for (int i = 0; i<(text[30]); i++)
if (isalpha(text[i]) == isalpha(text[i+1]))
numDoubles++;
cout << "There is "<< numDoubles << " character double.";
cin.get();cin.get();
return 0;
}
but it still dont work the output is always 0
So how do we know what data you're using?
And you still are not using code tags!
Code:
#include <iostream>
using namespace std;
int main ()
{
int numDoubles = 0;
char text[30];
cout<<"Enter sentence: ";
cin.getline(text,30);
for (int i = 0; i<(text[30]); i++)
if (isalpha(text[i]) == isalpha(text[i+1]))
numDoubles++;
cout << "There is "<< numDoubles << " character double.";
cin.get();cin.get();
return 0;
}
Do you see how this looks compared to what you've posted?
Now:
Code:
for (int i = 0; i<(text[30]); i++)
Explain what that loop does? What is that "text[30]" doing there? The function to get the length of a C-string is strlen(), so why are you not using it there?
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 21st, 2012 at 12:18 PM.
Bookmarks