how to count double characters ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: how to count double characters ?

  1. #1
    Join Date
    May 2012
    Posts
    4

    Exclamation how to count double characters ?

    delete
    Last edited by bekim; May 23rd, 2012 at 04:12 AM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,427

    Re: how to count double characters ?

    Quote Originally Posted by bekim View Post
    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

  3. #3
    Join Date
    Apr 1999
    Posts
    27,427

    Re: how to count double characters ?

    Quote Originally Posted by bekim View Post
    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

  4. #4
    Join Date
    Apr 1999
    Posts
    27,427

    Re: how to count double characters ?

    Quote Originally Posted by bekim View Post
    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.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,095

    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.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,095

    Re: how to count double characters ?

    Quote Originally Posted by bekim View Post
    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?

  7. #7
    Join Date
    May 2012
    Posts
    4

    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 ?

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,095

    Re: how to count double characters ?

    You could spell strlen correctly.

  9. #9
    Join Date
    May 2012
    Posts
    4

    Re: how to count double characters ?

    Quote Originally Posted by GCDEF View Post
    You could spell strlen correctly.
    i have post also the code you can see is n=stlen(text[30]); i think is correctly spelled

  10. #10
    Join Date
    Apr 1999
    Posts
    27,427

    Re: how to count double characters ?

    Quote Originally Posted by bekim View Post
    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

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,095

    Re: how to count double characters ?

    Quote Originally Posted by bekim View Post
    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.

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,095

    Re: how to count double characters ?

    Quote Originally Posted by Paul McKenzie View Post
    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

  13. #13
    Join Date
    May 2012
    Posts
    4

    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

  14. #14
    Join Date
    Apr 1999
    Posts
    27,427

    Re: how to count double characters ?

    Quote Originally Posted by GCDEF View Post
    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

  15. #15
    Join Date
    Apr 1999
    Posts
    27,427

    Re: how to count double characters ?

    Quote Originally Posted by bekim View Post
    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.

Page 1 of 2 12 LastLast

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center