for example
to ABCDEFGHIJKLMNOPQRSTUVWXYZCode:char array[] = "abcdefghijklmnopqrstuvwxyz"
thanks
for example
to ABCDEFGHIJKLMNOPQRSTUVWXYZCode:char array[] = "abcdefghijklmnopqrstuvwxyz"
thanks
Last edited by doongas; 10-05-2005 at 08:38 AM.
You could loop through the array and use the toupper function.
Another way to convert a string to uppercase is this:
Code:for(i=0; array[i]; i++) array[i] -= 32;
The idea is that 'A' is 32 ASCII characters behind 'a'. So if you subtract 32 from all lowercase characters, then your string will turn into uppercase. This is more efficient than calling toupper() because there's less overhead. However, only use this if you know that the entire content of array[] will be lowercase. If array will be mixed upper and lower case characters, use this code instead:
I've assumed that the string is null terminated.Code:for(i=0; array[i]; i++) if(array[i]>='a' && array[i]<='z') array[i] -= 32;
Last edited by Korg; 10-07-2005 at 04:20 AM.
Except toupper() doesn't assume that your character set is ASCII
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
As long as you're going to restrict yourself to the ASCII character set, you could do this too since bit operations are faster than subtraction:
Code:#include <stdio.h> #define mytoupper(c) ((c) >= 'a' && (c) <= 'z') ? (c) & ~0x20 : (c) int main(void) { char str[] = "I want to print this string in 'All Uppercase'."; int i; for(i = 0;str[i];++i) putchar(mytoupper(str[i])); putchar('\n'); return 0; }However, toupper() is preferred over a solution like this or the solution offered by the Korg since toupper() is guaranteed to be portable.Code:itsme@itsme:~/C$ ./mytoupper I WANT TO PRINT THIS STRING IN 'ALL UPPERCASE'. itsme@itsme:~/C$
Last edited by itsme86; 10-06-2005 at 11:28 AM.
If you understand what you're doing, you're not learning anything.
> you could do this too since bit operations are faster than subtraction:
Says who?
On most machines I know about, the basic arithmetic and bitwise operators take the same amount of time.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Indeed.Originally Posted by Salem
Code and timing using bitwise and:
Timing and code using subtract:Code:0m0.921s 0m0.919s 0m0.921s for (count = 0; count < 10000000L; count++) { int x; strcpy(buf, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); for (x = 0; x < 52; x++) buf[x] = buf[x] & ~0x20; }
Compile flags: gcc -O2 under Linux 2.6 on a P4 3.0Ghz machine.Code:real 0m0.920s real 0m0.920s real 0m0.922s for (count = 0; count < 10000000L; count++) { int x; strcpy(buf, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); for (x = 0; x < 52; x++) buf[x] = buf[x] - 0x20; }
Yes, I realise the subtract will fail on the already uppercase letters, but we're comparing performance, and even with the bitwise you'd need to check if was a letter.
On my computer XP with VC++ 6.0 (sp 5) the two were the same time in release mode but when compiled for debug mode they were slightly different (3359 versus 3406). The numbers are from GetTickCount(). Similar results with Dev-C++ 5.0. The time differences are so small as to render them insignificant.
My solution is simple, easy to understand and it works. It's a perfect solution for a beginner which I assume Doongas is.
The issues brought up about speed are negligible, unless Doongas is planning on converting billions of strings to uppercase.
itsme86's code is useful too but might be a little out of the beginner scope.
Salem's point is good. I assumed Doongas wasn't going to be working with other character sets. Beginner's often don't.
You are right, speed isn't likely to matter that much, so toupper would have sufficed, not your "subtract 32" trick.
Fair enough.
But I was trying to teach the guy something simple he might not know.
This is like teaching your dog to bark every time you fall asleep. I mean, sure, you could. But is it really a smart thing to do?Originally Posted by Korg
Quzah.
Hope is the first step on the road to disappointment.
sigh.
Why are you sighing? Your logic and reasoning was wrong. Being called on it was a very good thing. So don't go acting all butt hurt.Originally Posted by Korg
Thantos: ........ off.
Don't tell me what I can and can't do. And don't tell me about my logic and my reasoning.
I wasn't 'acting' anything.
You might have said it with a little more tact, you know? "I don't agree with you Korg". Not - "You are wrong". I have no time for people who have no diplomacy or tact.
quzah: are you saying it wasn't smart to try teach the new guy old tricks? What's so unintelligent about it? There's nothing unintelligent about it. Some things are more appropriate than others at different times. My solution wasn't stupid. Just a different approach.
It was there for him to use if he wanted it and ignore otherwise. No big deal. use toupper() if you like, for all I care.
Geez, where's an innocent child when you want to kill something?