"Inverting" ASCII characters

This is a discussion on "Inverting" ASCII characters within the C Programming forums, part of the General Programming Boards category; Hi, I want to invert some ASCII characters, ie, A becomes Z, z becomes A, etc. Would anyone be able ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    20

    "Inverting" ASCII characters

    Hi,

    I want to invert some ASCII characters, ie, A becomes Z, z becomes A, etc.

    Would anyone be able to point me to a simple ASCII chart or give me some suggestions?

    Thanks,

    Lewis

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    A good ASCII chart can be found at ASCIITable.com

    Edit: Also, here is a general concept of how this can be done.
    • If a character is upper case, add 32; 65 ('A') + 32 = 97 ('a');
    • If a character is lower case, subtract 32; 97 ('a') - 32 = 65 ('A');

    Feel free to use the ctype library functions, isupper() and islower().


    - Stack Overflow
    Last edited by Stack Overflow; 01-19-2005 at 09:44 AM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    That's cool, but I wanted to invert the letters. Ie. A becomes Z, b becomes y.
    You know what I mean?

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    That is simple.

    We know that lower case ASCII characters are between 97 and 122, and upper case ASCII characters are between 65 and 90.

    So, here's our concept:

    If lower case
    Find the difference between the character and the start. So, create an integer that equals your character subtract 97. Where 97 is 'a'.
    To invert it, take the difference and subtract it by 122 ('z').

    The algorithm works something like this:
    122 - (c - 97)

    Where c is your character.

    If upper case
    Find the difference between the character and the start. So, create an integer that equals your character subtract 65. Where 65 is 'A'.
    To invert it, take the difference and subtract it by 90 ('Z').

    The algorithm works something like this:
    90 - (c - 65)

    Where c is your character.

    Conclusion
    If you fill in the blanks, it should work. Let's try it here with the letter d:

    Upper case: 90 - ('D' - 65) = 90 - (68 - 65) = 90 - 3 = 87
    87 in ASCII is 'W'

    Lower case: 122 - ('d' - 97) = 122 - (100 - 97) = 122 - 3 = 119
    119 in ASCII is 'w'


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    That's not too tough. The following pseudocode should get you there:
    Code:
    If char_to_convert is between 65 and 90
       converted_char =  155 - char_to_convert
    If char_to_convert is between 97 and 122
       converted_char = 219 - char_to_convert
    Even though the 155 and 219 look like 'magic' numbers, they're pretty easy to come up with. They are just the sums of lower bound and the upper bound of the range. For instance, if you wanted to convert 0-9 in the same manner, you'd subtract the character from 105 (0 has ASCII value 48, 9 has ASCII value 57).

    [edit]Ha...which is pretty much exactly what Stack Overflow has for you.[/edit]
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    Thanks alot for your answers!

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Just think of a char as an int.

    char 'c' = 99
    'd' = 100

    So you could go like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
     int i;
     for(i=0; i<=255; i++) {
      printf("%i: %c\n", i, (char)i);
     }
    
     return 0;
    }
    Code:
    kleid@Shiva:~/Programming/Laboratory$ ./a.out
    0:
    1:  
    2:  
    3:  
    4:  
    5:
    6:  
    7:
    8:
    9:
    10:
    
    11:
    
    12:
    
    13:
    14:
    15:
    16:  
    17:  
    18:  
    19:  
    20:  
    21:  
    22:  
    23:  
    24:  
    25:  
    26:  
    27:  
    28:  
    29:  
    30:  
    31:  
    32:
    33: !
    34: "
    35: #
    36: $
    37: %
    38: &
    39: '
    40: (
    41: )
    42: *
    43: +
    44: ,
    45: -
    46: .
    47: /
    48: 0
    49: 1
    50: 2
    51: 3
    52: 4
    53: 5
    54: 6
    55: 7
    56: 8
    57: 9
    58: :
    59: ;
    60: <
    61: =
    62: >
    63: ?
    64: @
    65: A
    66: B
    67: C
    68: D
    69: E
    70: F
    71: G
    72: H
    73: I
    74: J
    75: K
    76: L
    77: M
    78: N
    79: O
    80: P
    81: Q
    82: R
    83: S
    84: T
    85: U
    86: V
    87: W
    88: X
    89: Y
    90: Z
    91: [
    92: \
    93: ]
    94: ^
    95: _
    96: `
    97: a
    98: b
    99: c
    100: d
    101: e
    102: f
    103: g
    104: h
    105: i
    106: j
    107: k
    108: l
    109: m
    110: n
    111: o
    112: p
    113: q
    114: r
    115: s
    116: t
    117: u
    118: v
    119: w
    120: x
    121: y
    122: z
    123: {
    124: |
    125: }
    126: ~
    127: 
    128: ?
    129: ?
    130: ?
    131: ?
    132: ?
    133: ?
    134: ?
    135: ?
    136: ?
    137: ?
    138: ?
    139: ?
    140: ?
    141: ?
    142:
    143:
    144: ?
    145: ?
    146: ?
    147: ?
    148: ?
    149: ?
    150: ?
    151: ?
    152: ?
    153: ?
    154: ?
    155: ?
    156: ?
    157: ?
    158: ?
    159: ?
    160: ?
    161: ?
    162: ?
    163: ?
    164: ?
    165: ?
    166: ?
    167: ?
    168: ?
    169: ?
    170: ?
    171: ?
    172: ?
    173: ?
    174: ?
    175: ?
    176: ?
    177: ?
    178: ?
    179: ?
    180: ?
    181: ?
    182: ?
    183: ?
    184: ?
    185: ?
    186: ?
    187: ?
    188: ?
    189: ?
    190: ?
    191: ?
    192: ?
    193: ?
    194: ?
    195: ?
    196: ?
    197: ?
    198: ?
    199: ?
    200: ?
    201: ?
    202: ?
    203: ?
    204: ?
    205: ?
    206: ?
    207: ?
    208: ?
    209: ?
    210: ?
    211: ?
    212: ?
    213: ?
    214: ?
    215: ?
    216: ?
    217: ?
    218: ?
    219: ?
    220: ?
    221: ?
    222: ?
    223: ?
    224: ?
    225: ?
    226: ?
    227: ?
    228: ?
    229: ?
    230: ?
    231: ?
    232: ?
    233: ?
    234: ?
    235: ?
    236: ?
    237: ?
    238: ?
    239: ?
    240: ?
    241: ?
    242: ?
    243: ?
    244: ?
    245: ?
    246: ?
    247: ?
    248: ?
    249: ?
    250: ?
    251: ?
    252: ?
    253: ?
    254: ?
    255: ?
    So if you want to manipulate chars, just pretend they're numbers. But that could not be very portable because it only would probably deal with the ASCII character set.
    Last edited by Kleid-0; 01-19-2005 at 05:25 PM.

  8. #8
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    An unsigned char has a value between 0 and 255.
    A signed char (or just the shorthand char) has a value between -128 and 127.

    A char can have a negative value because it is not necessarily interpreted as a written character. char is merely an integral datatype and like int, short, and long, char can have sign.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Stack Overflow
    Hello,

    An unsigned char has a value between 0 and 255.
    A signed char (or just the shorthand char) has a value between -128 and 127.
    Not exactly. The standard defines "minimum values" for each, not fixed numbers.

    A char has the following "minimum values":
    a) The low end is CHAR_MIN, which is either SCHAR_MIN or 0, depending on if it is signed by default or not.
    b) The high end is CHAR_MAX, which is either SCHAR_MAX, or UCHAR_MAX, again depending if it is signed or not.
    A signed char has the following "minimum values":
    a) The low end is SCHAR_MIN, which is -127.
    b) The high end is SCHAR_MAX, which is 127.
    An unsigned char has the following "minimum values":
    a) The low end is 0, because there is no UCHAR_MIN macro.
    b) The high end is UCHAR_MAX which must be ((2^CHAR_BIT)-1).
    And finally, CHAR_BIT must be a minimum of 8.

    Again, these are minimums, so it need not be some set value, just so long as it meets the minimum definitions.

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

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Kleid-0
    But that could not be very portable because it only would probably deal with the ASCII character set.
    Try to "portablize" it for fun.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void foo ( char *s )
    {
       static const char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       static const char lower[] = "abcdefghijklmnopqrstuvwxyz";
       while ( *s )
       {
          size_t i;
          if ( isupper ( *s ) )
          {
             for ( i = 0; i < sizeof upper - 1; ++i )
             {
                if ( *s == upper [ i ] )
                {
                   *s = upper [ ( sizeof upper - 2 ) - i ];
                   break;
                }
             }
          }
          else if ( islower ( *s ) )
          {
             for ( i = 0; i < sizeof lower - 1; ++i )
             {
                if ( *s == lower [ ( sizeof lower - 2 ) - i] )
                {
                   *s = lower [ i ];
                   break;
                }
             }
          }
          ++s;
       }
    }
    
    int main ( void )
    {
       static char text[] = "The Quick Brown Fox Jumps Over The Lazy Dog.";
       puts ( text );
       foo  ( text );
       puts ( text );
       return 0;
    } 
    
    /* my output
    The Quick Brown Fox Jumps Over The Lazy Dog.
    Gsv Jfrxp Yildm Ulc Qfnkh Levi Gsv Ozab Wlt.
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    More portable fun...
    Code:
    #include <stdio.h>
    int flopchar( int c )
    {
        switch( c )
        {
    #define C case
    #define R return
            C'a':R'z';C'A':R'Z';
            C'b':R'y';C'B':R'Y';
            C'c':R'x';C'C':R'X';
            C'd':R'w';C'D':R'W';
            C'e':R'v';C'E':R'V';
            C'f':R'u';C'F':R'U';
            C'g':R't';C'G':R'T';
            C'h':R's';C'H':R'S';
            C'i':R'r';C'I':R'R';
            C'j':R'q';C'J':R'Q';
            C'k':R'p';C'K':R'P';
            C'l':R'o';C'L':R'O';
            C'm':R'n';C'M':R'N';
            C'n':R'm';C'N':R'M';
            C'o':R'l';C'O':R'L';
            C'p':R'k';C'P':R'K';
            C'q':R'j';C'Q':R'J';
            C'r':R'i';C'R':R'I';
            C's':R'h';C'S':R'H';
            C't':R'g';C'T':R'G';
            C'u':R'f';C'U':R'F';
            C'v':R'e';C'V':R'E';
            C'w':R'd';C'W':R'D';
            C'x':R'c';C'X':R'C';
            C'y':R'b';C'Y':R'B';
            C'z':R'a';C'Z':R'A';
    
        }
        R 0;
    }
    
    int main( void )
    {
        int x,y;
        for( x = 'a', y = 'A'; x <= 'z'; x++,y++ )
            printf("%c is %c, %c is %c\n", x, flopchar( x ), y, flopchar( y ) );
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  3. ascii characters video displaying on tv screen
    By deian in forum C Programming
    Replies: 6
    Last Post: 10-12-2004, 09:46 PM
  4. Outputting ASCII characters?
    By Jamsan in forum C++ Programming
    Replies: 3
    Last Post: 03-24-2003, 06:14 PM
  5. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 09:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21