Kodewerx

Our culture has advanced beyond all that you could possibly comprehend with one hundred percent of your brain.
It is currently Thu Mar 28, 2024 7:08 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: a-z --> A-Z
PostPosted: Sun Oct 01, 2006 10:28 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:46 pm
Posts: 2331
Location: *poof*
Title: The Mad Hacker
C++

I tried this:
Code:
   if ((letter >= 33)&&(letter <= 58))
{
   letnum = int(letter) + 32;
   letter = char(letnum);
}
But it didn't work. Is there an easier way? ^_^

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Oct 01, 2006 10:33 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
This is a part of my "xstring" lib, which does upper case/lower case conversion, white space trimming, character replacement, sub-string searching, etc.
Code:
// Upper case routine
//  Returns number of characters modified
int str_ucase(char *str) {
   int i = 0;
   int j = 0;

   while (i < strlen(str)) {
      if ((str[i] >= 'a') && (str[i] <= 'z')) {
         str[i] &= ~0x20;
         j++;
      }
      i++;
   }
   return j;
}

_________________
I have to return some video tapes.

Feed me a stray cat.


Last edited by Parasyte on Sun Oct 01, 2006 10:35 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Oct 01, 2006 10:34 pm 
Offline
Kommunist
Kommunist
User avatar

Joined: Sun Oct 01, 2006 9:36 pm
Posts: 283
Keep in mind I'm a total newbie when it comes to C, but...assuming letter is a char, shouldn't you be using int(letter) inside the comparison?

EDIT: Nevermind. Para is the man when it comes to C.

_________________
"The man who trades freedom for security does not deserve nor will he ever receive either." -Benjamin Franklin


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Oct 01, 2006 10:44 pm 
a char is a signed short int.

PS: Para's post is what I would've have done, except:

Code:
str[i] -= 0x20;
inside the for loop. Using a NOT and an AND is likely faster though.


Top
  
Reply with quote  
 Post subject:
PostPosted: Sun Oct 01, 2006 10:47 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
A char is a signed char (8-bit)
A short is a signed short int (16-bit)
An int is a signed int (32-bit)
A long is a signed long int (64-bit)
A long long is a signed long long int (128-bit)

Of course, this all depends upon the implementation. These numbers expect a 64-bit architecture. (For 32-bit architecture, 'long' is 32-bit, and 'long long' is 64-bit.)

~ Bitwise operations and simple addition/subtraction are equally clocked on recent CPUs. (Made within the past decade or more.)

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Oct 02, 2006 12:00 am 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:46 pm
Posts: 2331
Location: *poof*
Title: The Mad Hacker
Thanks for trying to help guys!

I got it working though!

Code:
if ((letter >= 97)&&(letter <= 122))
{
   letter = letter - 32;
}


I'm going from lower case to caps and lowers have higher values than caps so I had to subtract 32. It also worked without the type casting. ^_^

easy n sleazy

woot
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
1111011110000111000000011100001111111100
0100000100011000110001100011001001100100
0100100100010000010001000001001001100100
0101110100110000011011000001101001100100
0101110100110000011011000001100001100000
0111011100110000011011000001100001100000
0111011100010000010001000001000001100000
0110001100011000110001100011000001100000
0110001100000111000000011100000111111000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Oct 02, 2006 12:31 am 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
Using character constants ('a' and 'z') instead of numbers is much more readable, by the way.

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Oct 02, 2006 8:11 am 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:46 pm
Posts: 2331
Location: *poof*
Title: The Mad Hacker
lol
If you're talking about the program code, it doesn't really matter as it is comented in the "full" program. ;p

And, lol, if you mean the output of the program, it's only a mid-point for what I'd like to do. ^_^

Thanks though, Parasyte. :)

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Oct 02, 2006 8:16 am 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
I am talking about source code readability, actually.

letter >= 'a'
vs.
letter >= 97

What the fuck is 97? I don't know without looking it up... But I know which letter 'a' is.

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Oct 02, 2006 8:46 am 
Parasyte wrote:
A char is a signed char (8-bit)
A short is a signed short int (16-bit)
An int is a signed int (32-bit)
A long is a signed long int (64-bit)
A long long is a signed long long int (128-bit)

Of course, this all depends upon the implementation. These numbers expect a 64-bit architecture. (For 32-bit architecture, 'long' is 32-bit, and 'long long' is 64-bit.)

~ Bitwise operations and simple addition/subtraction are equally clocked on recent CPUs. (Made within the past decade or more.)


Hmm.... interesting. I had always thought that a short was a byte. Would that be the case on 32-bit?


Top
  
Reply with quote  
 Post subject:
PostPosted: Mon Oct 02, 2006 8:52 am 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
'short' is always 16-bit, so far as I recall.

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Yandex [RuBot] and 183 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group