Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,320 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,106 people online right now. Registration is fast and FREE... Join Now!




Making the first letter of a string uppercase

 
Reply to this topicStart new topic

Making the first letter of a string uppercase

magicfrog
post 23 Feb, 2008 - 07:22 PM
Post #1


New D.I.C Head

*
Joined: 23 Feb, 2008
Posts: 2

i am currently working on a program and I have a string variable into which a person's name is read into. is there a C++ function that lets you capitalize the first letter in a string? I just need something simple, everything i have seen for this says you need to write your own function to do it. but alas my code is convulated enough as it is.

it is not a requirement for the program, but it would make it look better. any help would on is would be appreciated.
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 23 Feb, 2008 - 08:59 PM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(magicfrog @ 23 Feb, 2008 - 08:22 PM) *

is there a C++ function that lets you capitalize the first letter in a string?

Try reading the string into an array, & then issue toUpper on only the 1st element of the array.

That would be my solution.
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 23 Feb, 2008 - 09:16 PM
Post #3


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,923



Thanked 118 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


As no2 stated, the toUpper function on the first character in an array is probably the best approach. Be careful though, some implementations will actually use the Length() method, then loop through that, but this can be inefficient at times, so it may be best to use an iterator instead, something like this


cpp

#include <cctype>
#include <iostream>

string Capitalize(string str)
{
string::iterator it(str.begin());

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

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


Remember that toupper and tolower want their parameter converted to an unsigned char, which is why I'm converting it. That should give you a good start, happy coding smile.gif

User is offlineProfile CardPM

Go to the top of the page

Sn0wm4n
post 8 Jul, 2008 - 09:12 AM
Post #4


New D.I.C Head

*
Joined: 8 Jul, 2008
Posts: 11


My Contributions


I know that you probably don't need this anymore but I made a header file to do that. I made a header file like this and put it into the folder of my program where all of the other header files are like iostream and cstdlib. I then just put #include <Casechange.h> (name of the file) and then I could use the function, caseup and casedown with something like caseup(anygivenword[1]) or casedown(anygivenword[0].

CODE
#include <iostream>
#include <cstdlib>

using namespace std;

char caseup(char entry) {
    if (entry == 'a') {
       entry = 'A';
             }
    if (entry == 'b') {
       entry = 'B';
             }
    if (entry == 'c') {
       entry = 'C';
             }
    if (entry == 'd') {
       entry = 'D';
             }
    if (entry == 'e') {
       entry = 'E';
             }
    if (entry == 'f') {
       entry = 'F';
             }
    if (entry == 'g') {
       entry = 'G';
             }
    if (entry == 'h') {
       entry = 'H';
             }
    if (entry == 'i') {
       entry = 'I';
             }
    if (entry == 'j') {
       entry = 'J';
             }
    if (entry == 'k') {
       entry = 'K';
             }
    if (entry == 'l') {
       entry = 'L';
             }
    if (entry == 'm') {
       entry = 'M';
             }
    if (entry == 'n') {
       entry = 'N';
             }
    if (entry == 'o') {
       entry = 'O';
             }
    if (entry == 'p') {
       entry = 'P';
             }
    if (entry == 'q') {
       entry = 'Q';
             }
    if (entry == 'r') {
       entry = 'R';
             }
    if (entry == 's') {
       entry = 'S';
             }
    if (entry == 't') {
       entry = 'T';
             }
    if (entry == 'u') {
       entry = 'U';
             }
    if (entry == 'v') {
       entry = 'V';
             }
    if (entry == 'w') {
       entry = 'W';
             }
    if (entry == 'x') {
       entry = 'X';
             }
    if (entry == 'y') {
       entry = 'Y';
             }
    if (entry == 'z') {
       entry = 'Z';
             }
    return entry;
}

char casedown(char entry) {
    if (entry == 'A') {
       entry = 'a';
             }
    if (entry == 'B') {
       entry = 'b';
             }
    if (entry == 'C') {
       entry = 'c';
             }
    if (entry == 'D') {
       entry = 'd';
             }
    if (entry == 'E') {
       entry = 'e';
             }
    if (entry == 'F') {
       entry = 'f';
             }
    if (entry == 'G') {
       entry = 'g';
             }
    if (entry == 'H') {
       entry = 'h';
             }
    if (entry == 'I') {
       entry = 'i';
             }
    if (entry == 'J') {
       entry = 'j';
             }
    if (entry == 'K') {
       entry = 'k';
             }
    if (entry == 'L') {
       entry = 'l';
             }
    if (entry == 'M') {
       entry = 'm';
             }
    if (entry == 'N') {
       entry = 'n';
             }
    if (entry == 'O') {
       entry = 'o';
             }
    if (entry == 'P') {
       entry = 'p';
             }
    if (entry == 'Q') {
       entry = 'q';
             }
    if (entry == 'R') {
       entry = 'r';
             }
    if (entry == 'S') {
       entry = 's';
             }
    if (entry == 'T') {
       entry = 't';
             }
    if (entry == 'U') {
       entry = 'u';
             }
    if (entry == 'V') {
       entry = 'v';
             }
    if (entry == 'W') {
       entry = 'w';
             }
    if (entry == 'X') {
       entry = 'x';
             }
    if (entry == 'Y') {
       entry = 'y';
             }
    if (entry == 'Z') {
       entry = 'z';
             }
    return entry;
}



This post has been edited by Sn0wm4n: 8 Jul, 2008 - 12:53 PM
User is offlineProfile CardPM

Go to the top of the page

polymath
post 8 Jul, 2008 - 09:34 AM
Post #5


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


KISS:

cpp

#include <string>
//two overloads
void Cap(string& str) {
if (str.at(0)>=97 && str.at(0)<=122) str[0]=str.at(0)-32;
}

string Cap(string str) {
if (str.at(0)>=97 && str.at(0)<=122) str[0]=str.at(0)-32;
return (str);
}


I'm not that good with pointers, i think in the first overload i have to dereference str in str.at() and str[0]

Anyway, just another way to look at things
User is offlineProfile CardPM

Go to the top of the page

Cerolobo
post 8 Jul, 2008 - 09:35 AM
Post #6


D.I.C Regular

Group Icon
Joined: 5 Apr, 2008
Posts: 440



Thanked 31 times
My Contributions


Actually, you can implement the above code in a much more effiecent and simplifed way.

First, take a look at the ASCII Table.

CODE
#include <iostream>
#include <cstdlib>

using namespace std;

char caseup(char entry) {
    if(entry >= 'a' && entry <= 'z')
        entry -= 'a' - 'A';
    return entry;
}

char casedown(char entry) {
    if(entry >= 'A' && entry <= 'Z')
        entry += 'a' - 'A';
    return entry;
}


That's basically how toupper() and tolower() works. As for the if statment you posted, the checks you did aren't useful, since you actually do that check in caseup() and casedown().
User is offlineProfile CardPM

Go to the top of the page

polymath
post 8 Jul, 2008 - 11:33 AM
Post #7


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


QUOTE(Cerolobo @ 8 Jul, 2008 - 01:35 PM) *

Actually, you can implement the above code in a much more effiecent and simplifed way.

First, take a look at the ASCII Table.

CODE
#include <iostream>
#include <cstdlib>

using namespace std;

char caseup(char entry) {
    if(entry >= 'a' && entry <= 'z')
        entry -= 'a' - 'A';
    return entry;
}

char casedown(char entry) {
    if(entry >= 'A' && entry <= 'Z')
        entry += 'a' - 'A';
    return entry;
}


That's basically how toupper() and tolower() works. As for the if statment you posted, the checks you did aren't useful, since you actually do that check in caseup() and casedown().

Even better. Ignore what i said smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 02:29AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month