buffer to upper case?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 35
Reputation: toolbox03 is an unknown quantity at this point 
Solved Threads: 0
toolbox03 toolbox03 is offline Offline
Light Poster

buffer to upper case?

 
0
  #1
May 14th, 2008
How do I convert the characters in buffer to upper case?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,897
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: buffer to upper case?

 
0
  #2
May 14th, 2008
How about toupper()?

  1. #include <ctype.h>
  2. [...]
  3. int main()
  4. {
  5. char up, buffer = 'a';
  6. up = toupper(buffer);
  7. return 0;
  8. }

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

  1. char buffer = 'a';
  2. 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.
Last edited by niek_e; May 14th, 2008 at 5:09 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 35
Reputation: toolbox03 is an unknown quantity at this point 
Solved Threads: 0
toolbox03 toolbox03 is offline Offline
Light Poster

Re: buffer to upper case?

 
0
  #3
May 14th, 2008
Thanks for your help.

Originally Posted by niek_e View Post
How about toupper()?

  1. #include <ctype.h>
  2. [...]
  3. int main()
  4. {
  5. char up, buffer = 'a';
  6. up = toupper(buffer);
  7. return 0;
  8. }

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

  1. char buffer = 'a';
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,621
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: buffer to upper case?

 
0
  #4
May 15th, 2008
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:
  1. void changeBufferToUpper (char *buffer)
  2. {
  3. char * buffer_ptr = buffer;
  4. size_t buffer_len = strlen(buffer);
  5.  
  6. while(buffer_len--)
  7. buffer_ptr=toupper(buffer_ptr++);
  8. }

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.
Last edited by jephthah; May 15th, 2008 at 9:41 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: buffer to upper case?

 
0
  #5
May 16th, 2008
> 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:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6. char line[1024];
  7.  
  8. if (fgets(line, sizeof line, stdin) != NULL) {
  9. fputs(line, stdout);
  10. strupr(line);
  11. fputs(line, stdout);
  12. }
  13.  
  14. return 0;
  15. }
  1. ed rules!
  2. Original: ed rules!
  3. 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.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,621
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: buffer to upper case?

 
0
  #6
May 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: buffer to upper case?

 
2
  #7
May 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,621
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: buffer to upper case?

 
1
  #8
May 16th, 2008
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.


.
Last edited by jephthah; May 16th, 2008 at 5:24 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: buffer to upper case?

 
0
  #9
May 16th, 2008
Ya! Good to see me right in something.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,621
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 121
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: buffer to upper case?

 
0
  #10
May 18th, 2008
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:

  1.  
  2. void stringToUpper (char *buffer)
  3. // converts any lowercase characters in a string to uppercase
  4. // leaves non-lowercase and non-alpha character as they were
  5. //
  6. // input: pointer to buffer containing original string
  7. // output: pointer to modified buffer, original string is overwritten
  8.  
  9. {
  10. char * buffer_ptr = buffer;
  11. size_t buffer_len = strlen(buffer);
  12.  
  13. while(buffer_len--)
  14. *buffer_ptr=toupper(*buffer_ptr++);
  15. }


.
Last edited by jephthah; May 18th, 2008 at 5:50 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC