Thread: toupper(); tolower(); ?

  1. #1
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140

    Question toupper(); tolower(); ?

    hi there
    are there functions like toupper(); and tolower();
    which i know from php that i can use in C ?

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Sure, you can find them in:

    ctype.h

    Code:
    #include <ctype.h>
    Good luck

  3. #3
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    uh thanks!

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Should you ever be caught w/o ctype.h
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char aString[] = "THiS # Is && A && CRazY # strING\n";
       int i, letter;
       
       printf("%s", aString); 
             
       for(i=0; aString[i] != '\0'; i++)
       {
          letter = aString[i];
          /* to upper */
          if(letter >= 97 && letter <= 122)
          	aString[i] = letter - 32;
       }
       
       printf("toupper: %s", aString);
       
       for(i=0; aString[i] != '\0'; i++)
       {
          letter = aString[i];
          /* to lower */
          if(letter >= 65 && letter <= 90)
            aString[i] = letter + 32;
       }
       
       printf("tolower: %s", aString);
    
       return 0;
    }

  5. #5

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by ronin
    Should you ever be caught w/o ctype.h
    Code:
          if(letter >= 97 && letter <= 122)
          	aString[i] = letter - 32;
    
          if(letter >= 65 && letter <= 90)
            aString[i] = letter + 32;
       }
    Non portable. But then, why would you be without ctype.h in the first place?

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

  7. #7
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I enjoy thinking of ways to replicate the functions supported by the compiler, and I deal primarily with Dos instead of other platforms such as Unix, Linux, Win32 etc, so those methods have worked.

    It may be a lot of extra work, but it helps me to understand what's going on, and isn't that the purpose of learning in general?.... I've never taken a C course, but I've had one semester of intro C++

    You care to elaborate on the non portability? Is it the numbering systems?

    What about replacing int with a char and using 'a' && 'z' ?
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >You care to elaborate on the non portability?
    C does not assume ASCII; in EBCDIC, letters of the alphabet are not contiguous.

    >What about replacing int with a char and using 'a' && 'z' ?
    Better, but no -- still not portable.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>You care to elaborate on the non portability?
    You need to look at the different between the ASCII and EBCDIC charts.

    A simple observation is that in the ASCII chart, the uppercase A-Z are 65 to 90, and the lower case a-z are 97 to 122.

    Whereas, the EBCDIC chart has these in different places completely. Take a look at this table and you'll see that a-z are not even in one contiguous block, so addition/subtraction on them won't work the same as ASCII.

    [edit]Doh!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Hmm... I never thought about that.

    A new topic to hunt down and conquer.

    Thanks Dave... nice source, and as always thanks Hammer.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tolower and locale
    By MK27 in forum C Programming
    Replies: 16
    Last Post: 02-03-2009, 09:05 PM
  2. toupper() function
    By strokebow in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2008, 10:54 AM
  3. Need help with toupper and tolower function
    By phoebus in forum C Programming
    Replies: 8
    Last Post: 04-27-2008, 10:18 PM
  4. need example of toupper()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 06-29-2002, 10:07 AM
  5. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 02:43 PM