| | |
buffer to upper case?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
How about toupper()?
There is another way, although some people don't recommend using it, because it doesn't work on all charactersets
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.
c Syntax (Toggle Plain Text)
#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
c Syntax (Toggle Plain Text)
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.
Last edited by niek_e; May 14th, 2008 at 5:09 am.
•
•
Join Date: Apr 2008
Posts: 35
Reputation:
Solved Threads: 0
Thanks for your help.
•
•
•
•
How about toupper()?
c Syntax (Toggle Plain Text)
#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
c Syntax (Toggle Plain Text)
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:
as for nick's second suggestion,
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.
something like:
c Syntax (Toggle Plain Text)
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.
Last edited by jephthah; May 15th, 2008 at 9:41 pm.
> 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:
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.
A lot of compilers also have this kind of function as an extension to the standard library:
C Syntax (Toggle Plain Text)
#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; }
C Syntax (Toggle Plain Text)
ed rules! Original: ed rules! Upper case: ED RULES!
If at first you don't succeed, keep on sucking until you do succeed.
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.
"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.
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.
.
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.
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:
.
sorry for any confusion it caused anyone who tried to use it.
here's the correct version:
c Syntax (Toggle Plain Text)
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++); }
.
Last edited by jephthah; May 18th, 2008 at 5:50 pm.
![]() |
Similar Threads
- Java Help Needed....... (Java)
- help writing a string to textfile with fprintf (C)
- help needed badly (Java)
- Open Gl Programing Error (help)... (Game Development)
- write to text file using fprintf() (C)
- Using Global Low-Level Hooks Without Using A Dll (Computer Science)
- Word and Letter Count (C)
- MIPS - I'm Not getting the right answer :( Can anybody Review :) (Assembly)
Other Threads in the C Forum
- Previous Thread: Doubly Linked List Doesnt Work
- Next Thread: Debug assertion failed problem
| Thread Tools | Search this Thread |
#include adobe ansi api array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic fflush file fork forloop framework frequency getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest histogram inches include incrementoperators input iso kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming stack standard string strings systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






