switch case level

A

asit

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;
}
 
R

Richard Tobin

asit said:
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
 
D

dj3vande

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;
}
 
J

jameskuyper

asit said:
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.
 
W

Willem

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
 
A

Amandil

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
 
J

jameskuyper

(e-mail address removed) 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.
 
R

Randy Howard

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.
 
C

Chris Dollin

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top