-
May 21st, 2012, 11:07 AM
#1
how to count double characters ?
Last edited by bekim; May 23rd, 2012 at 04:12 AM.
-
May 21st, 2012, 11:22 AM
#2
Re: how to count double characters ?
Originally Posted by bekim
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.
Regards,
Paul McKenzie
-
May 21st, 2012, 11:27 AM
#3
Re: how to count double characters ?
Originally Posted by bekim
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?
Regards,
Paul McKenzie
-
May 21st, 2012, 11:29 AM
#4
Re: how to count double characters ?
Originally Posted by bekim
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.
-
May 21st, 2012, 11:38 AM
#5
Re: how to count double characters ?
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.
-
May 21st, 2012, 11:40 AM
#6
Re: how to count double characters ?
Originally Posted by bekim
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?
-
May 21st, 2012, 11:45 AM
#7
Re: how to count double characters ?
SO i write this combining some other exercise i have done before :
#include <iostream>
using namespace std;
int main ()
{
int numDoubles = 0;
char text[30],n;
cout<<"Enter sentence: ";
cin.getline(text,30);
n=stlen(text);
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
any idea what i can fix it ?
-
May 21st, 2012, 11:47 AM
#8
Re: how to count double characters ?
You could spell strlen correctly.
-
May 21st, 2012, 11:51 AM
#9
Re: how to count double characters ?
Originally Posted by GCDEF
You could spell strlen correctly.
i have post also the code you can see is n=stlen(text[30]); i think is correctly spelled
-
May 21st, 2012, 11:54 AM
#10
Re: how to count double characters ?
Originally Posted by bekim
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?
Regards,
Paul McKenzie
-
May 21st, 2012, 11:56 AM
#11
Re: how to count double characters ?
Originally Posted by bekim
i have post also the code you can see is n=stlen(text[30]); i think is correctly spelled
Your are incorrect in your thinking. You did get it right in your first piece of code.
-
May 21st, 2012, 11:56 AM
#12
Re: how to count double characters ?
Originally Posted by Paul McKenzie
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?
Regards,
Paul McKenzie
Singular/plural
-
May 21st, 2012, 12:08 PM
#13
Re: how to count double characters ?
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
-
May 21st, 2012, 12:08 PM
#14
Re: how to count double characters ?
Originally Posted by GCDEF
Singular/plural
ok.
Better to just say:
Code:
cout << "The number of doubles found: " << numDoubles << endl;
instead of getting into singular/plural.
Regards,
Paul McKenzie
-
May 21st, 2012, 12:11 PM
#15
Re: how to count double characters ?
Originally Posted by bekim
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|