switch case level

Discussion in 'C Programming' started by asit, Jan 14, 2008.

  1. asit

    asit Guest

    please modify to get the correct output.(switch case is compulsory)

    #include <stdio.h>

    int main()
    {
    char ch;
    printf("Enter any character : ");
    ch=getch();
    switch(ch)
    {
    case (ch>=65 && ch<=90):
    printf("Capital Letter\n");
    break;
    case (ch>=97 && ch<=122):
    printf("Small Case Letter\n");
    break;
    case (ch>=48 && ch<=57):
    printf("Digit\n");
    break;
    default:
    printf("Any other character");
    }
    return 0;
    }
    asit, Jan 14, 2008
    #1
    1. Advertising

  2. In article <>,
    asit <> wrote:

    > switch(ch)
    > {
    > case (ch>=65 && ch<=90):
    > printf("Capital Letter\n");
    > break;


    Switch doesn't provide matching of ranges, so it's not the right
    thing to use for this kind of problem.

    Also, for most purposes you would be better off using the library
    functions isupper() etc, rather than assuming ASCII character values.

    >(switch case is compulsory)


    Not in real life.

    -- Richard
    --
    :wq
    Richard Tobin, Jan 14, 2008
    #2
    1. Advertising

  3. asit

    Guest

    In article <>,
    asit <> wrote:
    >please modify to get the correct output.(switch case is compulsory)
    >
    >#include <stdio.h>
    >
    >int main()
    >{
    > char ch;
    > printf("Enter any character : ");
    > ch=getch();


    You haven't shown us the definition of getch(). (I assume it's not
    entirely unlike getchar()?)

    > switch(ch)
    > {
    > case (ch>=65 && ch<=90):
    > printf("Capital Letter\n");
    > break;
    > case (ch>=97 && ch<=122):
    > printf("Small Case Letter\n");
    > break;
    > case (ch>=48 && ch<=57):
    > printf("Digit\n");
    > break;
    > default:
    > printf("Any other character");
    > }
    > return 0;
    >}


    Case arguments need to be constant expressions.
    The Right Way to do range checks is to not use a switch (and The Right
    Way to identify digits and case of letters isn't to use range checks),
    but if the switch is non-negotiable:
    --------
    #include <ctype.h>
    #include <stdio.h>

    /*Not compiled or tested. Caveat emptor.*/

    int main(void)
    {
    char ch;
    printf("Enter any character: "); fflush(stdout);
    ch=getchar();

    switch(isupper(ch))
    {
    case 0:
    switch(islower(ch))
    {
    case 0:
    switch(isdigit(ch))
    {
    case 0:
    printf("Other character\n");
    break;
    default:
    printf("Digit\n");
    break;
    }
    break;
    default:
    printf("Lower-case letter\n");
    break;
    }
    break;
    default:
    printf("Upper-case letter\n");
    break;
    }

    return 0;
    }
    --------


    dave

    --
    Dave Vandervies dj3vande at eskimo dot com

    [T]he only real 'crippled environment' I have a problem with is my own mind.
    --Gary S. Callison in the scary devil monastery
    , Jan 14, 2008
    #3
  4. asit

    Guest

    asit wrote:
    > please modify to get the correct output.(switch case is compulsory)
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > char ch;
    > printf("Enter any character : ");
    > ch=getch();
    > switch(ch)
    > {
    > case (ch>=65 && ch<=90):


    The label of a switch statement must be an integer constant
    expression. Every case label in your program involves 'ch', therefore
    preventing it from being a constant. You may be thinking about other
    languages such as perl where the restrictions on case labels are
    different.

    > printf("Capital Letter\n");
    > break;
    > case (ch>=97 && ch<=122):
    > printf("Small Case Letter\n");
    > break;
    > case (ch>=48 && ch<=57):
    > printf("Digit\n");
    > break;
    > default:
    > printf("Any other character");
    > }
    > return 0;
    > }


    The fact that your assignment requires you to use a switch statement
    is rather arbitrary. I can only see two bad ways to do it: the really
    bad way is to list every single letter or digit character as a case
    label, with fall-through. The moderately bad way to do so is to use
    switch() as a dumb substitute for if(). There might be a better way to
    do this that I haven't thought of yet. I'll give you a starting hint
    on how to use the less bad approach:

    #include <ctype.h>


    switch(isupper(ch))
    {
    case 0: // insert code for upper case letters.
    break;
    default: // insert code for characters that aren't upper case letters
    }

    You'll need multiple switch() statements to use this approach.
    , Jan 14, 2008
    #4
  5. asit

    Willem Guest

    asit wrote:
    ) please modify to get the correct output.(switch case is compulsory)
    )
    ) #include <stdio.h>
    )
    ) int main()
    ) {
    ) char ch;
    ) printf("Enter any character : ");
    ) ch=getch();
    ) switch(ch)
    ) {
    ) case (ch>=65 && ch<=90):
    ) printf("Capital Letter\n");
    ) break;
    ) case (ch>=97 && ch<=122):
    ) printf("Small Case Letter\n");
    ) break;
    ) case (ch>=48 && ch<=57):
    ) printf("Digit\n");
    ) break;
    ) default:
    ) printf("Any other character");
    ) }
    ) return 0;
    ) }

    Here's one option (NB:UB for non-ASCII chars):

    switch((ch>>4)&((6*((ch&64)&&(((ch+5)&31)/6)))|!((ch-42)>>4))) {
    case 6:
    printf("Capital Letter\n");
    break;
    case 4:
    printf("Small Case Letter\n");
    break;
    case 1:
    printf("Digit\n");
    break;
    default:
    printf("Any other character");
    }

    I'm sure it can be simplified even further, as currently the default case
    will always have the switch value be == 0.

    HTH, HAND.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Jan 14, 2008
    #5
  6. asit

    Amandil Guest

    On Jan 14, 12:36 pm, asit <> wrote:
    > please modify to get the correct output.(switch case is compulsory)


    That would seem to many to be referring to the assignment your teacher
    gave you. In standard C, switch is _not_ compulsary.

    > #include <stdio.h>
    >
    > int main()
    > {
    > char ch;
    > printf("Enter any character : ");
    > ch=getch();


    As Dave pointed out, getch() is not standard C, you probably want
    getchar().

    > switch(ch)
    > {
    > case (ch>=65 && ch<=90):


    This is illegal in Std C. The closest you can get is a gcc extension
    that allows
    ranges:
    case (65...90)
    > printf("Capital Letter\n");
    > break;
    > case (ch>=97 && ch<=122):


    Again, a gcc extension:
    case (97...122)
    > printf("Small Case Letter\n");
    > break;
    > case (ch>=48 && ch<=57):

    Once more, in gcc only (unless any other compiler understands it):
    case (48...57)
    > printf("Digit\n");
    > break;
    > default:
    > printf("Any other character");
    > }
    > return 0;
    >
    > }


    This gcc extension is not standard C. If your instructor is teaching
    you Std C, he/she's teaching you incorrectly. If you're being taught
    gcc, you probably ought to ask elsewhere (gnu.gcc.help).

    -- Marty
    Amandil, Jan 14, 2008
    #6
  7. asit

    Guest

    wrote:
    ....
    > ... There might be a better way to
    > do this that I haven't thought of yet. ...


    I was right about that.

    I was trying to avoid giving you a complete answer, because you'll
    learn a lot more by filling in the details yourself. However, since
    multiple complete answers have already been provided, I'll toss mine
    in:

    switch(islower(ch) ? 0 : isupper(ch) ? 1 : isdigit(ch) ? 2 : 3)
    {
    case 0:
    printf("Lower Case Letter\n");
    break;
    case 1:
    printf("Upper Case Letter\n")
    break;
    case 2:
    printf("Digit\n");
    break;
    default:
    printf("Any other character\n");
    break;
    }

    Some people would unnecessarily parenthesize the ?: operations to
    clarify the order of operations:

    islower(ch) ? 0 : (isupper(ch) ? 1 : (isdigit(ch) ? 2 : 3))

    However, the designers of C were very careful to define the grammar
    of ?: to allow chaining in precisely this fashion without needing
    parentheses. For me, inserting parentheses would actually make it less
    readable. YMMV.
    , Jan 14, 2008
    #7
  8. asit

    Randy Howard Guest

    On Mon, 14 Jan 2008 16:08:22 -0600, Martin Ambuhl wrote
    (in article <>):

    > asit wrote:
    >> please modify to get the correct output.(switch case is compulsory)
    >>
    >> #include <stdio.h>
    >>
    >> int main()
    >> {
    >> char ch;
    >> printf("Enter any character : ");
    >> ch=getch();
    >> switch(ch)
    >> {
    >> case (ch>=65 && ch<=90):
    >> printf("Capital Letter\n");
    >> break;

    >
    > This is a joke, right? You have no way of knowing that the encoding on
    > the target machine is such that capital letters are in the range 65-90.


    Of course he does. He is very likely to have a way of knowing what it
    is on a given target machine. What he doesn't know is whether or not
    it will work on any /other/ machine.

    > And the case statement is a joke as well. (ch >= 75 && ch <- 90) is not
    > an integral constant, so cannot be a case label.


    True enough.


    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
    Randy Howard, Jan 14, 2008
    #8
  9. asit

    Chris Dollin Guest

    wrote:

    > The fact that your assignment requires you to use a switch statement
    > is rather arbitrary. I can only see two bad ways to do it: the really
    > bad way is to list every single letter or digit character as a case
    > label, with fall-through. The moderately bad way to do so is to use
    > switch() as a dumb substitute for if(). There might be a better way to
    > do this that I haven't thought of yet. I'll give you a starting hint
    > on how to use the less bad approach:


    If it's having a switch statement that's demanded, just insert:

    switch (0) { case 0:; }

    somewhere in the code.

    --
    Gaming Hedgehog
    "Based on their behaviour so far -- I have no idea" /Sahara/
    Chris Dollin, Jan 15, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Bolotski

    Switch level simulation package

    Mike Bolotski, Aug 18, 2003, in forum: VHDL
    Replies:
    1
    Views:
    1,404
    VhdlCohen
    Aug 19, 2003
  2. gane kol

    switch case with enum

    gane kol, May 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,289
    Kevin Spencer
    May 6, 2005
  3. Brian J. Sayatovic

    Why constant ints in switch case expressions?

    Brian J. Sayatovic, Jul 2, 2003, in forum: Java
    Replies:
    22
    Views:
    21,786
    Dale King
    Jul 9, 2003
  4. pabbu
    Replies:
    8
    Views:
    699
    Marc Boyer
    Nov 7, 2005
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    88
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page