473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

switch case level

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
6 4508
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the...
10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
5
by: Bryan Parkoff | last post by:
C++ programmers and I tried to experience by writing 65536 switch cases. It is too large for Windows XP and other operating system to handle. It looks like that JMP Table obtains 256K bytes for...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
11
by: hasadh | last post by:
Hi, is the assemly code for if..else and switch statements similar. I would like to know if switch also uses value comparison for each case internally or does it jump to the case directly at...
7
by: cytec123187 | last post by:
Hello, I am working on an Adobe Acrobat file that uses javascript for calculations. I am trying to create a field that uses two other fields to determine a number value. I think this requires...
9
by: yndygo | last post by:
Alright then - I know it's got to be out there somewhere, but I can't find it... I'm looking at a piece of code and there's some debate as to what the behavior will be. Given a switch/case...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.