Alphabet not changing

This is a discussion on Alphabet not changing within the C Programming forums, part of the General Programming Boards category; This compiles ok, but im getting wrong results. Im am trying to print the alphabet twice, once in uppercase, then ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Alphabet not changing

    This compiles ok, but im getting wrong results. Im am trying to print the alphabet twice, once in uppercase, then call a function that changes the character size to lowercase and prints it back.

    My output is showing them both as uppercase letters. I cant see anything wrong with my convert function. Is it just some silly mistake I have made?

    Heres what I have done:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    /*function prototype*/
    void convert ( char* );
    
    /*main function - begins program execution -----------------------------------*/
    int main ( void )
    {
       char let;
    
       for ( let = 'A'; let <= 'Z'; let++ )
       {
          printf("%2c", let);
       }
    
       convert ( &let );
    
       printf("\n\n");
    
       for ( let = 'A'; let <= 'Z'; let++ )
       {
          printf("%2c", let);
       }
    
       getchar(); /*freeze console output window*/
    
       return 0; /*return value from int main*/
    }
    
    void convert ( char *sPtr )
    {
       while ( *sPtr != '\0' )
       {
          if ( toupper ( *sPtr ))
          {
             *sPtr = islower ( *sPtr );
          }
    
          sPtr++;
       }
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    On your second for loop have this instead:

    Code:
    for (let = 'a'; let <= 'z'; let++)
    {
    ... // stuff
    }
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    char let;
    ...
    convert ( &let );
    'let' isn't a string, its a single character, so your really only converting the last character ('Z').

    islower() returns TRUE (!0) on if it's lower-case and FALSE (0) if its not.
    http://www.mkssoftware.com/docs/man3/islower.3.asp
    Therefore, the convert(&let) turns 'Z' into !0 ...

    You're not actually converting case...

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Thank you! I knew it was somthing stupid like that. I think I overlooked the second loop case. Cheers again

    EDIT

    Zacs7 I will amend my code. Good point and well spotted. I need new glasses
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why not store it in an array?

    Code:
    char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int i;
    
    printf("Uppercase: &#37;s\n", Alphabet);
    printf("Lowercase: ");
    for(i = 0; i < sizeof(Alphabet)-1; i++)
    {
        putchar(tolower(Alphabet[i]));
    }
    printf("\n");
    or something...

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >      if ( toupper ( *sPtr ))
    >      {
    >         *sPtr = islower ( *sPtr );
    >      }
    Should be:
    Code:
          if ( isupper ( *sPtr ))
          {
             *sPtr = tolower ( *sPtr );
          }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. changing alphabet order
    By firipu in forum C Programming
    Replies: 2
    Last Post: 12-17-2007, 12:56 PM
  2. shorter way to represent alphabet
    By cdkiller in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2006, 12:38 PM
  3. Changing windows without changing?
    By Lionmane in forum Windows Programming
    Replies: 7
    Last Post: 10-19-2005, 11:41 AM
  4. [C++/WinAPI] Changing bitmap contrast
    By jagi in forum Windows Programming
    Replies: 0
    Last Post: 03-27-2005, 02:51 PM
  5. i,j, n,m, x,y,z ... We dont know the alphabet
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-20-2002, 04:46 AM

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