By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

switch case level

P: n/a
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;
}
Jan 14 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In article <80**********************************@t1g2000pra.g ooglegroups.com>,
asit <li*****@gmail.comwrote:
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
Jan 14 '08 #2

P: n/a
In article <80**********************************@t1g2000pra.g ooglegroups.com>,
asit <li*****@gmail.comwrote:
>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 '08 #3

P: n/a
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
Jan 14 '08 #4

P: n/a
On Jan 14, 12:36 pm, asit <lipu...@gmail.comwrote:
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
Jan 14 '08 #5

P: n/a
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.

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

Kill your switch statement entirely.
Include <ctype.h>, and then use the standard isupper(), islower(),
isdigit() functions.
Jan 14 '08 #6

P: n/a
On Mon, 14 Jan 2008 16:08:22 -0600, Martin Ambuhl wrote
(in article <5v*************@mid.individual.net>):
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

Jan 14 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.