Counting number of lowercase.[NEWBIE]

This is a discussion on Counting number of lowercase.[NEWBIE] within the C++ Programming forums, part of the General Programming Boards category; hi, i am new in this forum. i have a problem with my code. can someone correct it for me ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    Smile Counting number of lowercase.[NEWBIE]

    hi, i am new in this forum. i have a problem with my code. can someone correct it for me and give some advice.

    i want to find out the number of lowercase letters used in the name...

    this is the code tat i stuck with....

    For instance:

    Code:
    int main()
    {
        string name;
        
        cout<<"Enter name: ";
        cin>>name;
        
        for(int i=0; name[i]; i++)
        {
           if(islower(name[i]))
           {
             int sum = 0;
             
             sum++;
             cout<<sum;
           }
        }
    and the output is 11 if i enter [Ben] as my input...
    i want to count the it so the ouput will display 2.
    can someone help me on this please.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Why you're initalizing your array to 0 and outputing inside of the loop?

    [EDIT] Sorry it took me so long to come to this conculsion. I'm dead tired right now.
    Last edited by SlyMaelstrom; 11-30-2005 at 06:43 AM.
    Sent from my iPadŽ

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    why are you displaying sum inside your if statement? which is inside your for statement? at this point you do not yet want to display sum. declare sum outside of your conditionals (up at the top by where you declare name wouldn't be a bad spot ) and then move cout<<sum; to just after the closing of your for statement. (btw, that may not be your complete code, but you need another closing bracket at the end and a return 0; wouldn't be a bad idea)
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Basically, if you don't quite understand your output, I can tell you that it was correct. Just think about how you have your code.

    You input "Ben"

    Everytime it finds a lowercase letter, it sets sum to zero. Adds 1 to it and outputs that number. It did that one 'e' and it did that on 'n'. 1 and 1 is what? 11.
    Sent from my iPadŽ

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i want to find out the number of lowercase letters used in the name...
    The easy way:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <cctype>
    
    int main()
    {
      std::string name = "Julienne";
    
      std::cout<< std::count_if ( name.begin(), name.end(), islower ) <<'\n';
    }
    The safe easy way:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <cctype>
    
    struct is_lower {
      int operator() ( int c )
      {
        return islower ( c );
      }
    };
    
    int main()
    {
      std::string name = "Julienne";
    
      std::cout<< std::count_if ( name.begin(), name.end(), is_lower() ) <<'\n';
    }
    Why does the safe version put a seemingly unnecessary wrapper around islower? Because cctype declares a one argument version of islower, but iostream also declares a two argument version for use with locales. By passing just the name islower, which is overloaded in two headers that are both included, this creates an ambiguity that could cause problems for you. So a unique wrapper is used to call the single argument version and ensure that everything plays nicely. That said, the easy way works just peachy on a lot of compilers.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> for(int i=0; name[i]; i++)

    The string class is not null terminated and can have embedded nulls. Don't use name[i] as the control of the loop. Use i < name.length() or i < name.size(). Otherwise you might end up with a crash by passing an invalid index to the string's operator[].

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by Daved
    >> for(int i=0; name[i]; i++)

    The string class is not null terminated and can have embedded nulls. Don't use name[i] as the control of the loop. Use i < name.length() or i < name.size(). Otherwise you might end up with a crash by passing an invalid index to the string's operator[].
    I had that in my original post but wasn't quite sure if it was as invalid as I thought it was. Thanks for clearing that up.
    Sent from my iPadŽ

  8. #8
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Quote Originally Posted by Prelude
    The safe easy way:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <cctype>
    
    struct is_lower {
      int operator() ( int c )
      {
        return islower ( c );
      }
    };
    
    int main()
    {
      std::string name = "Julienne";
    
      std::cout<< std::count_if ( name.begin(), name.end(), is_lower() ) <<'\n';
    }
    Could you also do something like:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <cctype>
    #include <string>
    
    bool is_lower(string&);
    
    int main()
    {
      std::string name = "Julienne";
    
      std::cout<< std::count_if ( name.begin(), name.end(), is_lower() ) <<'\n';
    }
    
    bool is_lower(string& c)
    {
        return islower(c);
    }
    to eliminate the problem with the overloaded islower function? If so, how do they differ, and what situations would each be more suited to?
    There is a difference between tedious and difficult.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Could you also do something like:
    Why would you have to do something like that? By passing a string to count_if, each item passed to the predicate is a single character, so a reference to a string is a type mismatch. You're also suggesting basically the same thing that I did except using a function instead of a function object.

    But yes, I did forget to include <string> in my examples.
    My best code is written with the delete key.

  10. #10
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Quote Originally Posted by Prelude
    By passing a string to count_if, each item passed to the predicate is a single character, so a reference to a string is a type mismatch.
    oops. So if I were to do it this way, I should use a char instead, or...?

    Quote Originally Posted by Prelude
    You're also suggesting basically the same thing that I did except using a function instead of a function object.
    That's what I was wondering. I don't know much about structs or classes yet, and so far setting up a new predicate as I did is the only way I've learned to do it.

    Thanks.
    There is a difference between tedious and difficult.

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I'm supprised no one has mentioned anything like this:

    Code:
    int main()
    {
        string name;
        
        cout<<"Enter name: ";
        cin>>name;
        
        for(int i=0, length=name.size(); i<length; i++)
        
           if(name[i] >= 'a' && name[i] <= 'z')
    
                  sum++;
    
           cout<<sum;
    
         return 0;
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by The Brain
    I'm supprised no one has mentioned anything like this:
    I'm not. You could do that, but when there is a predefined function for you, what's the point?
    Sent from my iPadŽ

  13. #13
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Quote Originally Posted by SlyMaelstrom
    I'm not. You could do that, but when there is a predefined function for you, what's the point?
    because its usually smaller to do things yourself rather than including a header file that may contain quite a bit of code that you dont really need. its like including string.h when the only thing your using it for is to see how many chars are in a string
    Registered Linux User #380033. Be counted: http://counter.li.org

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    On the Dev-C++ compiler.

    Code:
    #include <cctype>
    int main() {}
    // 17,535 bytes
    Code:
    int main() {}
    // 17,535 bytes
    Even if it did make a small difference. This isn't 1965, people aren't worried about a handful of bytes on their harddrive.
    Last edited by SlyMaelstrom; 11-30-2005 at 07:31 PM.
    Sent from my iPadŽ

  15. #15
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    than including a header file that may contain quite a bit of code that you dont really need.
    AFAIK, the preprocessor will only pull the code you use from the header, so that's not an issue.
    There is a difference between tedious and difficult.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  2. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 02:21 PM
  3. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  4. Counting Number of days from year zero
    By wireless in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2002, 07:31 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21