How do I convert the characters in buffer to upper case?

Recommended Answers

All 9 Replies

How about toupper()?

#include <ctype.h>
[...]
int main()
{
    char up, buffer = 'a';
    up = toupper(buffer);
    return 0;
}

There is another way, although some people don't recommend using it, because it doesn't work on all charactersets

char buffer = 'a';
buffer += '0'; /* add 48 to the char */

I haven't encountered any systems where the code above wouldn't work, but I still recommend using the first solution.
It works on all systems, and it reads a lot easier which is important especially when you have to share your code with other people.

Thanks for your help.

How about toupper()?

#include <ctype.h>
[...]
int main()
{
    char up, buffer = 'a';
    up = toupper(buffer);
    return 0;
}

There is another way, although some people don't recommend using it, because it doesn't work on all charactersets

char buffer = 'a';
buffer += '0'; /* add 48 to the char */

I haven't encountered any systems where the code above wouldn't work, but I still recommend using the first solution.
It works on all systems, and it reads a lot easier which is important especially when you have to share your code with other people.

nick's first suggestion is good for a single character, but you'd have to write a function to convert an entire string.

something like:

void changeBufferToUpper (char *buffer)
{
    char * buffer_ptr = buffer;
    size_t buffer_len = strlen(buffer);

    while(buffer_len--)
        buffer_ptr=toupper(buffer_ptr++);
}

as for nick's second suggestion, buffer += '0'; /* add 48 to the char */ ... that will work for the case described, but will likely cause your program to blow up.

i mean, im sure nick only meant to suggest this for the case where the character being modified is always and forever a lowercase letter... but that's not necessarily true. To do it this way, you'd have to employ additional and significant checking on the char beforehand. which is what "toupper" does for you.

> but you'd have to write a function to convert an entire string.
A lot of compilers also have this kind of function as an extension to the standard library:

#include <stdio.h>
#include <string.h>

int main()
{
  char line[1024];

  if (fgets(line, sizeof line, stdin) != NULL) {
    fputs(line, stdout);
    strupr(line);
    fputs(line, stdout);
  }

  return 0;
}
ed rules!
Original: ed rules!
Upper case: ED RULES!

If you want your code to be maximally portable, it's better not to use extensions. But keep in mind that extensions are more likely to be written better than something you would make, and they have the benefit of being able to take more advantage of the compiler.

your first comment

"If you want your code to be maximally portable, it's better not to use extensions."

i believe is the most correct.

at work i use National Instruments' C compiler (LabWindows/CVI), and they have a ton of useful libraries


but they're a double-edged sword. they make life easier for the moment, but most of which will never port to either GCC or MSVC, and i become dependent upon them at my own peril. God forbid i have to develop a GUI front end outside of CVI.

I am confused looking at this thread. To convert a lower case letter to upper case I always subtracted 32 from the lowercase letter. Adding 48? Is this correct or is it a mistake.
Please reply.

commented: good catch +1
commented: Thanks for the correction. You're right +6

yes, you're absolutely right Prabakar.

funny thing is, i looked at it and didnt even notice. I was thinking how such a conversion could be applied incorrectly -- like what happens when you try and make an EOF or CR or LF or NULL character "uppercase"

I suspect Nick was thinking of converting decimal values to ASCII, and had a late-night brainfart.

that's what i claim when i do stuff like that, anyhow.


.

commented: You're right, my brain misfired +6

Ya! Good to see me right in something.

i just realized i forgot the pointer dereferencers. i should know better than to just type code off the top of my head without testing it.

sorry for any confusion it caused anyone who tried to use it.

here's the correct version:

void stringToUpper (char *buffer)
// converts any lowercase characters in a string to uppercase
// leaves non-lowercase and non-alpha character as they were
//
// input:  pointer to buffer containing original string
// output: pointer to modified buffer, original string is overwritten

{
    char * buffer_ptr = buffer;
    size_t buffer_len = strlen(buffer);

    while(buffer_len--)
        *buffer_ptr=toupper(*buffer_ptr++);
}

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.