Thread: Sample C code that change character

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    49

    Sample C code that change character

    Hi all,
    First of all, i have to make it clear that i am not asking anyone to do my homework.

    All i want is, do anyone have sample C code that change all the lower case chacter to upper case and vis versa.
    For example, a file that will contain all the characters, space and punctation, the C code will take in the file and change all the upper case to lower and lower case to upper case instead of using the toupper() and to lower().

    input => This is an Example.
    output => tHIS IS AN eXAMPLE.
    diana --> programming is tough

  2. #2
    FOX
    Join Date
    May 2005
    Posts
    188
    Use the functions isupper(), islower(), toupper() and tolower() from ctype.h.


    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    static char *strtolower(const char *src)
    {
            char *dest = malloc(sizeof(char) * (strlen(src)+1));
    
            int i = 0;
            while (src[i])
            {
                    dest[i] = isupper(src[i]) ? tolower(src[i]) : toupper(src[i]);
                    ++i;
            }
            dest[i] = '\0';
            return dest;
    }
    
    int main(void)
    {
            char *p = strtolower("Hello, World!");
            printf("%s\n", p);
            return 0;
    }
    Last edited by ^xor; 05-29-2005 at 03:48 AM. Reason: Improved code

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Wouldn't it have been clearer to just use dest[i] = src[i] etc, rather than introduce another variable and another style into the function?
    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.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Or just really simply:
    Code:
    itsme@itsme:~/C$ cat switchcase.c
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      int c;
    
      while((c = getchar()) != EOF)
        putchar(isupper(c) ? tolower(c) : toupper(c));
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ cat file.txt
    ThiS Is teXt wITH
    rANDom CAPItaliZatiOn
    itsme@itsme:~/C$ ./switchcase < file.txt
    tHIs iS TExT With
    RandOM capiTALIzATIoN
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  5. #5
    FOX
    Join Date
    May 2005
    Posts
    188
    I wasn't sure if toupper/tolower would try to convert non-alpha characters, but looking at your solution it appears they don't.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      int c;
    
      while((c = getchar()) != EOF)
        putchar(isupper( (unsigned char) c) ? tolower( (unsigned char) c) : toupper( (unsigned char) c));
    
      return 0;
    }

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I don't know about your compiler, but the prototypes for those ctype.h functions on my system are:

    int isupper(int c);
    int toupper(int c);
    int tolower(int c);

    So what's with the typecasting?

    [edit]
    I just stumbled across this in the man page for the ctype.h functions:
    If c is not an unsigned char value, or EOF, the behaviour
    of these functions is undefined.
    So I guess that's why you added the typecasting. However, the getchar() man page mentions:
    [getchar()] reads the next character from stream and returns
    it as an unsigned char cast to an int, or EOF
    on end of
    file or error.
    So since I'm not changing the value between the getchar() and the tolower()/toupper() calls, it's safe without the typecasting anyway.
    [/edit]
    Last edited by itsme86; 05-29-2005 at 03:17 AM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    Ya i have tried and both give the same out put...

    But both uses the function "toupper()" and "tolower()", is there any examples that does not use this two given functions and still produce the same result...

    Can anyone tell me what does "? and :" in the "while" loop means.

    Thanks....
    diana --> programming is tough

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> So since I'm not changing the value between the getchar() and the tolower()/toupper() calls, it's safe without the typecasting anyway. <<

    Yep, I wasn't aware of that requirement of getchar (I should have checked). As a pattern, I think it is a good idea to always use the cast with these functions. At least that's my pathetic excuse...

    >> Can anyone tell me what does "? and :" in the "while" loop means. <<

    This is called the conditional expression operator. It it a shortcut version of if/else. You can find more info with this search.

  10. #10
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Quote Originally Posted by dianazheng
    is there any examples that does not use this two given functions and still produce the same result...
    you can check this http://web.cs.mun.ca/~michael/c/ascii-table.html

    Basically what you will notice is that to convert a character from uppercase to lowercase you will have to add 32 (decimal) to the decimal value of the upper case letter.

    To go from lower case to uppercase just subtract 32 (decimal) to get the decimal representation of the uppercase letter.

    Just involves some if structures to make sure you are not dealing with non-letter characters.

    This way you dont need any function like toupper or tolower - actually it makes me remember these stupid exercises during my assembler class

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > is there any examples that does not use this two given functions
    And when your character set isn't ASCII, you'll be glad you used tolower() etc rather than some hack involving adding or subtracting 32.
    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.

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    Ya, i know that its better to use the functions available.

    But i just want to know that how can we do that without using the available function.

    Also how the code will look like and exposed myself to that.
    diana --> programming is tough

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SMS sample code
    By hagenuk in forum C Programming
    Replies: 1
    Last Post: 05-30-2008, 11:47 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  4. The code sample of classes in the tutorial. :o
    By Wall in forum C++ Programming
    Replies: 3
    Last Post: 08-20-2004, 09:18 AM
  5. How to return the ASCII code value of a character in C?
    By DramaKing in forum C Programming
    Replies: 3
    Last Post: 02-11-2002, 12:06 PM