Thread: Having a problem with a Switch Statement

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    17

    Having a problem with a Switch Statement

    1. Ask for a single character to be typed from the keyboard. Analyze the
    character and print out a message that it is:
    - an alphabetic character (a–z or A–Z)
    - a digit (0–9) or
    - a special character (anything else)

    * Tried a couple of ways of using a switch statement to code this problem just havent been able to figure out an efficient way, the exercise wants me to use an "if" or a "switch statement". I would appreciate your perspective and insight on how to takle this problem...

    thank you

    Arod

    More details...

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[])
    {
        char let;
        
        printf("Please Insert a single character here: \n");
        scanf("%c",&let);
        
        
        switch(let)
        {
            case '0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case'8':case'9':
                printf("You Entered a Digit from 0-9\n");
                break;
            case 'a':case'b':case'c':case'd':case'e':case'f':case'g':case'h':case'i':case'j':
            case 'A':case'B':case'C':case'D':case'E':case'F':case'G':case'H':case'I':case'J':
            case 'k':case'l':case'm':case'n':case'o':case'p':case'q':case'r':case's':case't':
            case 'K':case'L':case'M':case'O':case'P':case'Q':case'R':case'S':case'T':case'U':    
                printf("You Entered a Character from A-Z\n");
                break;    
            default:
                printf("Special Character");
        }
        return 0;
    }
    I am going try and do it as an if statement, any suggestions?
    Last edited by ARod609; 07-13-2011 at 04:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-30-2011, 01:59 PM
  2. Problem with a switch statement.
    By Merholtz in forum C Programming
    Replies: 3
    Last Post: 10-19-2008, 04:21 PM
  3. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  4. Replies: 1
    Last Post: 08-31-2004, 04:07 AM

Tags for this Thread