Thread: Convert to title case

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    I'd build a list of prepositions and articles just to make sure that you don't capitalize those unless it's the first word (as long as we're striving for correctness). You can then compare parts of the title, and if they don't match one of the normally lowercase words you can capitalize the first initial.

  2. #17
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Cambridge, UK
    Well, i dint wanted to give you the whole code. You will have to sit down and go through the problem again and again. You have already got lots of suggestion from many other members. Although just to give you a brief idea, i mean an alternative idea on how to approach this problem. Look at the following code

        ptr = strtok( str, " " );
        while( ptr != NULL )
               strcat( result, toTitleCase( ptr ) );
               strcat( result, " ");
               ptr = strtok( NULL, " " );
    Well, the main objective of the problem is to capitalize the char in a given word. Lets keep things simple here. An alternative way to approach is to token the string you get each word. You know that in each word the first char should be capital. Sent that word to some user defined function which changes the first char in that word to capital and the lower cases the rest of the chars in the word and return that string back to the calling function. And returned string will .be concatenated to a resultant string. May be i can provided that user defined function. But you should work with the rest.
    const char *toTitleCase( char *token )
         int i;
         token[0] = toupper( token [0] );
         for( i = 1; token[i] != '\0'; i++ )
              token[i] = tolower( token[i] );
         return token;
    If this donst help, then you should really look back and go through some basics again!!


  3. #18
    Registered User
    Join Date
    Oct 2007
    Quote Originally Posted by Elysia View Post
    It's best you do when showing code
    No bad practices!
    I assume these are his homework so I haven't published the entire code.
    My code works excellent, just give me the permission and I'll put it here.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Quote Originally Posted by Mole42
    I don't like having a parameter as both a 'in' and an 'out' - I think they should be one or the other. So I wouldn't be modifying the original string, but rather returning the modified version. Then again, I don't like having to free data allocated in a previous function either, so I would typically either return a buffer to a local static variable, or request a secondary buffer in the function itself for the result (and returning the secondary buffer).
    The concept of an out parameter that is also an in parameter seems fine to me, especially since it is used in the C standard library itself (and the C++ standard library too, by way of iterators), but requesting a separate out buffer seems fine too, though it comes at the price of an extra copy that may have been unnecessary.

    Quote Originally Posted by eXeCuTeR
    My code works excellent, just give me the permission and I'll put it here.
    Hopefully gertie will give you the permission by posting his/her own attempt first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM
  2. Converting Numbers to Words
    By denizengt in forum C Programming
    Replies: 20
    Last Post: 11-05-2003, 09:19 PM
  3. Basic Data types continue
    By viryonia in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 10:21 AM
  4. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM