Thread: Code for UPPER and REVERSE

  1. #1
    Unregistered
    Guest

    Code for UPPER and REVERSE

    A user inputs a string of lower case alphabetic letters, and i need to output the string in reverse order in upper case, ie. "abcd" becomes DCBA. However, im not allowed to use any library functions. Can anyone tell me what code i would need to do these two (upper and reverse) functions manually?

    Thanks

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Printing the string in reverse is easy. Simply place a pointer on the last character of your string and move backwards. You will want to know the length of the string in advance, so you don't fall of the end (or in this case, the front) of the string.

    You can convert lower case to upper with the bitwise AND operator:
    Code:
    char c = 'a';
    
    c &= 223;  /* The value held by c is now 'A' */
    Jason Deckard

  3. #3
    Unregistered
    Guest
    thanks, but can you elaborate on the &= 223 part please? Im unsure as to what that number means

  4. #4
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Certainly. In the ASCII character set, the only difference between any lower case letter and its capital counterpart is a single bit (the bit representing the value 32). For example:
    • a = 01100001
      A = 01000001

      j = 01101010
      J = 01001010

    You can use the bitwise AND operator to flip a single bit:
    • 01100001 (a)
      11011111 (223)
      --------- (&)
      01000001 (A)

    There was a good discussion about bitwise operators in general in this thread.
    Jason Deckard

  5. #5
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101
    Code:
    // ** this loop gets the length of a_string
    do
    {
    	an_int++; // ** when declaring this make it zero
    }
    while(a_string[an_int-1] != '\0');
    
    
    
    // ** this loop reverses and capitalizes a_string into a_new_string
    for(another_int = 0; an_int-1 > 0; an_int--)
    {
    	a_new_string[another_int] = a_string[an_int-2]-32;
    	another_int++;
    }
    Sometimes, the farthest point from the center is the center itself.

    Your life is your canvas, it's only as beautiful as you paint it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 08:33 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  4. Problem with my string reverse code
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-24-2005, 09:22 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM