Thread: Capitalize first letter of a word (function)

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    55

    Capitalize first letter of a word (function)

    I need to pass a word to a function and have the first letter in that word "displayed" back to the user as a capital letter, the rest lower case.

    I say "displayed" because I DO NOT want to change the original.

    I have the following function, which works, but I think there's a memory leak, and, this looks more complicated than what it needs to be.

    Code:
    // This will CAP the first letter of a single word
    char *CAP_WORD(char *txt)
    {
    	std::string rt = "";
    	char *txt2 = "";
    	rt = txt;	
    	txt2 = const_cast<char*>(rt.c_str());
    	_strlwr(txt2);
    	*txt2 = (((*txt2)>='a'  && (*txt2) <= 'z') ? ((*txt2)+('A'-'a')) : (*txt2));
    	return (_strdup(txt2));
    }
    I DO NOT want the CAP_WORD function to alter the original.

    As the function sits right now, it does not affect the original. However, I feel there's memory that's getting built that I cannot free.

    I'm ready to answer any questions needed.

    Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's C++. I'll see if someone will move this for you.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    Not necessarily. It's more C than C++. It's a mix of both.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It is impossible for you to win this argument with me, so stop trying, because it's not even an argument. It's a fact: That is C++.

    Remember in school when they explained true and false questions to you? If any part of it is false, the whole statement is false. If any part of that is C++, all of it is C++.

    But just to humor you:
    Code:
    // This will CAP the first letter of a single word
    This is C++. Technically it could be C99, but this style of comment originated with C++, that makes it more C++ than C.
    Code:
    char *CAP_WORD(char *txt)
    {
    C++ has pointers, and char, so does C. This can go either ay, so we will ignore it.
    Code:
        std::string rt = "";
    That is completely C++.
    Code:
        char *txt2 = "";
    That could be either, so we will ignore it.
    Code:
        rt = txt;
    That's C++ because you are using an overloaded assignment operator to be able to assign to that string class.
    Code:
        txt2 = const_cast<char*>(rt.c_str());
        _strlwr(txt2);
    That cast is purely C++, so is the method for the class you are invoking. While technically you could have a function pointer as part of a structure, you and I both know that is C++.
    Code:
        *txt2 = (((*txt2)>='a'  && (*txt2) <= 'z') ? ((*txt2)+('A'-'a')) : (*txt2));
    That could be either.
    Code:
        return (_strdup(txt2));
    }
    I'm assuming you are saying that is C, even though standard C doesn't have _strdup.

    Anyway, as I've pointed out, that is C++. You win no part of this argument, stop trying.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    "...It is impossible for you to win this argument with me, so stop trying, because it's not even an argument..."

    Wow, and where did all that pent up anger come from? I personally don't recall having an argument at all with you. Relax. Really, it's okay. I understand your point.

    About the only one thing you were accurate on is this part: "..., because it's not even an argument..."

    I agree, it's not.

    Now then... is there someone a little less hyper-paranoid who could answer my original question?

    Thanks.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by xwielder View Post
    I personally don't recall having an argument at all with you.
    Then you are an idiot.

    You: Help!
    Me: That's C++, I'll get it moved to the C++ forum for you.
    You: It'S C!

    There. That was your argument.
    Quote Originally Posted by xwielder View Post
    About the only one thing you were accurate on is this part: "..., because it's not even an argument..."
    Yep, it's confirmed. You're an idiot.

    Everything I said about your code was true. All the things I pointed out that were C++ were in fact C++. Also, the part about true/false? That was true.
    Quote Originally Posted by xwielder View Post
    Now then... is there someone a little less hyper-paranoid who could answer my original question?

    Thanks.
    There was no paranoia there. You were wrong, and you posted in the wrong spot. Nothing I said was wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    Yeah, typical. Heh, I suppose every forum has 'em. After taking a brief moment to read through some of your recent responses to posts, your arrogance and rudeness toward others is quite evident.

    For me, it's simple. This will be my last reply to pretty much anything you could possibly ever post. Sad too, really. This just means that the people who are here to truly help have to read through all your ranting. A real shame that someone like yourself, who probably in reality is very brilliant, feels the need to flame like you do.

    Really, it's cool though. No harm no foul. Just words, right?

    Be cool, stay safe.

    Cheers.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a hint: Next time someone tells you that you posted in the wrong forum, and that they will have it moved to the right forum for you, don't turn it into an argument. Also, learn to tell the difference between C++ and C. Finally, learn to admit when you are wrong.

    Nobody here cares if you put me on ignore. No one at all.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by xwielder View Post
    Not necessarily. It's more C than C++. It's a mix of both.
    The minute you include even one C++ statement in a source code it won't compile in C anymore and thus it is all C++.

    This should be moved.

    You really picked on the wrong guy here, my friend.
    Last edited by CommonTater; 10-05-2011 at 08:48 PM.

  10. #10
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Wow...

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rmatze View Post
    Wow...
    It happens here sometimes... Programmers are a very direct bunch. It's an offshoot of the work they do... letter perfect, case sensitive, precision grammar does that to you. I usually appreciate their directness and even get their jokes. But when someone comes along who can't handle correction there's usually a direct nose dive as you saw here. It's a bit awkward but the bottom line is, if you want to play in the kitchen you have to get along with the chef.

    The op was being wildly oversensitive to the totally appropriate suggestion that his code would not compile in C so belonged in the C++ forum. Quzah, like many of us, has very little patience for that kind of unnecessary drama...

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's C++ - moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Anyway, now that we are clear that this is indeed C++, I suggest something like:
    Code:
    // This will capitalise the first letter of a single word
    std::string cap_word(std::string word)
    {
        if (!word.empty())
        {
            word[0] = std::toupper(word[0]);
        }
        return word;
    }
    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

  14. #14
    Registered User
    Join Date
    Jul 2010
    Posts
    86
    Um, I hate to say it, but as soon as name calling began, we left stern correction valley and went straight into douchebag ville. That is all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. make a char display a word or letter
    By dyelax in forum C++ Programming
    Replies: 12
    Last Post: 10-13-2009, 11:55 AM
  2. Capitalize first letter of every word in .txt file
    By crazygopedder in forum C Programming
    Replies: 9
    Last Post: 10-15-2008, 12:09 PM
  3. identifying first letter of word
    By speedyboy in forum C Programming
    Replies: 11
    Last Post: 03-19-2006, 07:56 AM
  4. Capatalizing the first letter of a word
    By cprog in forum C Programming
    Replies: 0
    Last Post: 12-07-2002, 06:58 PM
  5. Problem with letter and word counting
    By wordup in forum C Programming
    Replies: 3
    Last Post: 10-09-2002, 04:02 PM