Converting to uppercase in C++

John picture John · Jan 1, 2012 · Viewed 19.8k times · Source

Let's say you have:

const char * something = "m";

How would one make this uppercase, using toupper (or something else, if applicable)?

I want to use a char * instead of a string (I can use a string, but then I have to use str.c_str()).

So, how can I make char * something = "m"; contain "M"?

Answer

cnicutar picture cnicutar · Jan 1, 2012

I find you choice of C strings disturbing.. but anyway.

You can't change a string literal (char *something). Try an array:

char something[] = "m";
something[0] = toupper(something[0]);

To change an entire string:

char something[] = "hello";
char *p = something;

while (*p) {
    *p = toupper(*p);
    p++;
}