Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - How do I capitolise the first character of a string in C++? | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
Nebetsu

Shmee
Level: 55

Posts: 1238/1574
EXP: 1291130
For next: 23059

Since: 09-01-04
From: Nebland

Since last post: 3 hours
Last activity: 1 hour
Posted on 04-06-05 06:03 AM Link | Quote
How do I capitolise the first character of a string in C++?
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 4039/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 04-06-05 06:15 AM Link | Quote
I don't know if there's a specific function to do it, but something like this should work:

if((MyString[0] >= 'a') && (MyString[0] <= 'z')) //Between a and z?
MyString[0] -= 0x20; //Subtract difference between 'a' and 'A'

Assuming you're using char arrays, and not some C++ thing I don't know about.
Dish

Spiny
Level: 38

Posts: 324/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 04-06-05 06:28 AM Link | Quote
Instead of -= 0x20 -- I'd just use character constants:

MyString[0] += 'A' - 'a';

so you don't have to remember an exact value (this might also work if you change charsets too -- but don't take my word on that one).

But yeah -- either way you get the result.
Parasyte

Bullet Bill
Level: 35

Posts: 418/514
EXP: 267348
For next: 12588

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 04-06-05 06:36 AM Link | Quote
And don't forget to check that the char is actually a lowercase letter... You don't want to 'translate' any non-alpha characters or capital letters.
Banedon

Giant Red Paratroopa
Level: 55

Posts: 1143/1408
EXP: 1291380
For next: 22809

Since: 03-15-04
From: Michigan

Since last post: 101 days
Last activity: 90 days
Posted on 04-07-05 02:09 AM Link | Quote
Not sure about C++, but I know that C has the toupper function, which should work properly even when the character is not lowercase.
Dish

Spiny
Level: 38

Posts: 325/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 04-07-05 02:57 AM Link | Quote
oh yeah -- I forgot about those XD

But yeah -- since C++ includes all of C, that'd work in C++ too.
Nebetsu

Shmee
Level: 55

Posts: 1241/1574
EXP: 1291130
For next: 23059

Since: 09-01-04
From: Nebland

Since last post: 3 hours
Last activity: 1 hour
Posted on 04-07-05 09:57 AM Link | Quote
Thank you everyone. I used HyperHacker's method because it makes more sense than Disch's method. Sorry Disch. Your method is good and would make more sense to a normal mind, but I think HyperHacker's makes more sense because I understand how the ASCII chart works and stuff. It takes the ascii character number and subtracts 0x20 from it and then gives you the new character. Very nice method HyperHacker.
Dish

Spiny
Level: 38

Posts: 327/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 04-07-05 10:38 AM Link | Quote
HH's way was a shorter version of mine. Or rather I should say mine was a longer method of his, since he brought it up first.

To clarify the logic behind mine (even though you already pretty much have it solved -- I still feel compelled to explain mine so that the concept is understood):

A character in single quotes (such as 'A') is a character constant. It's replaced with the numerical value representing that character at compiletime. To illustrate this with pseudo-code (assuming ASCII char set):


char ch;

ch = 'A'; //this line has the same result as the line below
ch = 0x41;

if( 'A' == 0x41 )
{
//this condition is true, code here would execute
}

if( 'a' == 0x61 )
{
//also true
}

if( ('a' - 'A') == 0x20 )
{
//also true
}


My code essentially does the exact same thing as HH's, it just doesn't make you remember the exact numerical difference between upper and lowercase letters in the charset.

MyString[0] += 'A' - 'a';

... is the same as ...

MyString[0] += 0x41 - 0x61;

... is the same as ...

MyString[0] += -0x20;

... is the same as ...

MyString[0] -= 0x20;


The only difference is I dodged the use of a numerical constant (0x20) and used easier-to-remember character constants ('A' , 'a') with some simple logic math (add the target character, subtract the current character -- or add the difference between the two).

It's the same principle as converting a numerical charater ("0 1 2 3 4 5 6 7 8 9") to a numerical value.


char ch = '6';

int num;

num = ch - '0'; //this will yield 6
num = ch - 0x30; // this will also yield 6



They both do the same thing -- but the former doesn't require you to remember an arbitrary value.
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - How do I capitolise the first character of a string in C++? | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.006 seconds.