Function to capitalize string

Discussion in 'C++' started by Rick, Nov 16, 2003.

  1. Rick

    Rick Guest

    For some reason my function to capitalize the first letter in a string
    and keep all the other letters lowercase isn't working. This is what I
    have.

    string convname(string name)
    {
    int len;
    len = name.length();
    name[0] = toupper(name[0]);
    for(int i = 0; i < len; i++)
    name = tolower(name);
    return name;
    }
    Rick, Nov 16, 2003
    #1
    1. Advertising

  2. Rick wrote:
    > For some reason my function to capitalize the first letter in a string
    > and keep all the other letters lowercase isn't working. This is what I
    > have.
    >
    > string convname(string name)
    > {
    > int len;
    > len = name.length();
    > name[0] = toupper(name[0]);
    > for(int i = 0; i < len; i++)
    > name = tolower(name);
    > return name;
    > }
    >


    string convname(string name)
    {
    int len;
    len = name.length();
    if ( len > 0 )
    {
    name[0] = toupper(name[0]);
    for(int i = 1; i < len; i++)
    name = tolower(name);
    }
    return name;
    }

    issues:

    a) you referenced name[0] even though the string may be 0 length

    b) the loop started at position 0 - overwriting the first toupper
    assignment.
    Gianni Mariani, Nov 16, 2003
    #2
    1. Advertising

  3. Rick

    Rick Guest

    Gianni Mariani wrote:
    > Rick wrote:
    >
    >> For some reason my function to capitalize the first letter in a string
    >> and keep all the other letters lowercase isn't working. This is what
    >> I have.
    >>
    >> string convname(string name)
    >> {
    >> int len;
    >> len = name.length();
    >> name[0] = toupper(name[0]);
    >> for(int i = 0; i < len; i++)
    >> name = tolower(name);
    >> return name;
    >> }
    >>

    >
    > string convname(string name)
    > {
    > int len;
    > len = name.length();
    > if ( len > 0 )
    > {
    > name[0] = toupper(name[0]);
    > for(int i = 1; i < len; i++)
    > name = tolower(name);
    > }
    > return name;
    > }
    >
    > issues:
    >
    > a) you referenced name[0] even though the string may be 0 length
    >
    > b) the loop started at position 0 - overwriting the first toupper
    > assignment.
    >


    Thanks. Guess I overlooked small details.
    Rick, Nov 16, 2003
    #3
  4. Rick

    Moonlit Guest

    "Rick" <> wrote in message
    news:ZPQtb.3364$Ue4.1003@fed1read01...
    > For some reason my function to capitalize the first letter in a string
    > and keep all the other letters lowercase isn't working. This is what I
    > have.
    >
    > string convname(string name)
    > {
    > int len;
    > len = name.length();
    > name[0] = toupper(name[0]);
    > for(int i = 0; i < len; i++)
    > name = tolower(name);
    > return name;
    > }
    >

    for(int i = 1; i < len; i++)

    Notice the 1 instead of zero.

    Regards, Ron AF Greve.
    Moonlit, Nov 16, 2003
    #4
  5. "Rick" <> wrote in message
    news:ZPQtb.3364$Ue4.1003@fed1read01...
    > For some reason my function to capitalize the first letter in a string
    > and keep all the other letters lowercase isn't working. This is what I
    > have.
    >
    > string convname(string name)
    > {
    > int len;
    > len = name.length();
    > name[0] = toupper(name[0]);
    > for(int i = 0; i < len; i++)
    > name = tolower(name);
    > return name;
    > }
    >


    the for loop needs to start at 1, not zero
    Thomas Wintschel, Nov 16, 2003
    #5
  6. Rick

    Rolf Magnus Guest

    Gianni Mariani wrote:

    > Rick wrote:
    >> For some reason my function to capitalize the first letter in a
    >> string
    >> and keep all the other letters lowercase isn't working. This is what
    >> I have.
    >>
    >> string convname(string name)
    >> {
    >> int len;
    >> len = name.length();
    >> name[0] = toupper(name[0]);
    >> for(int i = 0; i < len; i++)
    >> name = tolower(name);
    >> return name;
    >> }
    >>

    >
    > string convname(string name)
    > {
    > int len;
    > len = name.length();
    > if ( len > 0 )
    > {
    > name[0] = toupper(name[0]);
    > for(int i = 1; i < len; i++)
    > name = tolower(name);
    > }
    > return name;
    > }
    >
    > issues:
    >
    > a) you referenced name[0] even though the string may be 0 length
    >
    > b) the loop started at position 0 - overwriting the first toupper
    > assignment.


    Another issue that might be visible or not on your system, depending on
    the signedness of char: touppoer and tolower want their parameter
    converted to an unsigned char. So actually has to be:


    string convname(string name)
    {
    int len;
    len = name.length();
    if ( len > 0 )
    {
    name[0] = toupper((unsigned char)name[0]);
    for(int i = 1; i < len; i++)
    name = tolower((unsigned char)name);
    }
    return name;
    }

    Oh, and calling length() on a string and then using that for a loop to
    iterate through it can be quite inefficient on some implementations
    (just the same as strlen for C style strings). You can work around that
    by using iterators:

    string convname(string name)
    {
    string::iterator it(name.begin());

    if (it != name.end())
    name[0] = toupper((unsigned char)name[0]);

    while(++it != name.end())
    {
    *it = tolower((unsigned char)*it);
    }
    return name;
    }


    All code untested, but should mostly work.
    Rolf Magnus, Nov 16, 2003
    #6
  7. Rick

    sunshine82

    Joined:
    Nov 11, 2007
    Messages:
    2
    How can I write this program using printf and scanf???

    I am taking a c++ course.I want to write this program:
    capitalize first letter of words
    for examlpe:jason go to school
    Jason Go To School
    all the first letters will be capital letter.
    I see a program this forum but the teacher didn't teach touper,tolower statements so I want to to write this program using printf and scanf like this ok???
    please help me immediately ??? I will have an exam on tuesday!!!
    sunshine82, Nov 11, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jay
    Replies:
    1
    Views:
    19,038
    Marc Dzaebel
    Feb 25, 2004
  2. Skip Montanaro

    Re: string capitalize sentence

    Skip Montanaro, Jun 24, 2005, in forum: Python
    Replies:
    0
    Views:
    418
    Skip Montanaro
    Jun 24, 2005
  3. Konstantin Veretennicov

    Re: string capitalize sentence

    Konstantin Veretennicov, Jun 24, 2005, in forum: Python
    Replies:
    0
    Views:
    513
    Konstantin Veretennicov
    Jun 24, 2005
  4. Iñaki Baz Castillo

    String#capitalize more complex

    Iñaki Baz Castillo, Apr 2, 2009, in forum: Ruby
    Replies:
    5
    Views:
    127
    Iñaki Baz Castillo
    Apr 2, 2009
  5. Rails List
    Replies:
    4
    Views:
    210
    Rails List
    Jun 5, 2009
Loading...

Share This Page