Thread: how do u convert array of characters to lowercase?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    how do u convert array of characters to uppercase?

    for example

    Code:
    char array[] = "abcdefghijklmnopqrstuvwxyz"
    to ABCDEFGHIJKLMNOPQRSTUVWXYZ

    thanks
    Last edited by doongas; 10-05-2005 at 08:38 AM.

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    You could loop through the array and use the toupper function.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    Another way

    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:


    Code:
    for(i=0; array[i]; i++) 
      if(array[i]>='a' && array[i]<='z') array[i] -= 32;
    I've assumed that the string is null terminated.
    Last edited by Korg; 10-07-2005 at 04:20 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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;
    }
    Code:
    itsme@itsme:~/C$ ./mytoupper
    I WANT TO PRINT THIS STRING IN 'ALL UPPERCASE'.
    itsme@itsme:~/C$
    However, toupper() is preferred over a solution like this or the solution offered by the Korg since toupper() is guaranteed to be portable.
    Last edited by itsme86; 10-06-2005 at 11:28 AM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Salem
    Says who?
    On most machines I know about, the basic arithmetic and bitwise operators take the same amount of time.
    Indeed.

    Code and timing using bitwise and:
    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;
    }
    Timing and code using subtract:

    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;
    }
    Compile flags: gcc -O2 under Linux 2.6 on a P4 3.0Ghz machine.

    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.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    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.

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You are right, speed isn't likely to matter that much, so toupper would have sufficed, not your "subtract 32" trick.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    Fair enough.

    But I was trying to teach the guy something simple he might not know.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Korg
    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?


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    sigh.

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Korg
    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.

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 09-24-2008, 06:16 PM
  2. copying the first 10 characters to an array?
    By arya6000 in forum C Programming
    Replies: 3
    Last Post: 09-20-2008, 10:39 PM
  3. convert char array to int array
    By skeme in forum C Programming
    Replies: 10
    Last Post: 09-07-2008, 10:37 PM
  4. Convert string of characters to integers
    By Digital_20 in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2008, 09:40 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM