Hi there,
I had a question. Is there any way of testing a string value in a
switch statement. I have about 50 string values that can be in a string
variable. I tried cheking them with the if else statements but it
looked pretty ugly and the string values to be tested is still
increasing. The switch statement seems to be a better choice then the
if else statement. But it seems that the switch statement accepts
integer values as the test condition. Can anybosy help me here or give
some idea ?
For eg,
switch(nameofIn stitution){
case UofCanada: printf( " U of canada\n");
break;
case UofIndia: printf( " U of India\n");
break;
............... ....
............... ..
............... ......
about 50 such cases
default: printf(" Sorry, this university is not
listed\n");
die();
break;
}
Thank you,
Priya 7 22126
priyanka wrote:
Hi there,
I had a question. Is there any way of testing a string value in a
switch statement. I have about 50 string values that can be in a string
variable. I tried cheking them with the if else statements but it
looked pretty ugly and the string values to be tested is still
increasing. The switch statement seems to be a better choice then the
if else statement. But it seems that the switch statement accepts
integer values as the test condition. Can anybosy help me here or give
some idea ?
For eg,
switch(nameofIn stitution){
case UofCanada: printf( " U of canada\n");
break;
case UofIndia: printf( " U of India\n");
break;
............... ...
............... .
............... .....
about 50 such cases
default: printf(" Sorry, this university is not
listed\n");
die();
break;
}
You could try something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *accepted_strin gs[] = {
"string 0",
"string 1",
"string 2",
};
int
select_str(char *s)
{
int i;
for (i=0; i < sizeof accepted_string s/sizeof *accepted_strin gs;
i++)
if (!strcmp(s, accepted_string s[i]))
return i;
return -1;
}
int
main(int argc, char **argv)
{
char *test;
int idx;
if (argc<2)
test = "test";
else
test = argv[1];
switch( idx = select_str(test )) {
case 0: /* Fallthru */
case 1: /* Fallthru */
case 2: /* Fallthru */
printf("selecte d %d\n", idx);
break;
default:
printf("unknown string\n");
break;
}
return EXIT_SUCCESS;
}
priyanka wrote:
Hi there,
I had a question. Is there any way of testing a string value in a
switch statement. [...]
This is Question 20.17 in the comp.lang.c Frequently
Asked Questions (FAQ) list http://c-faq.com/
--
Eric Sosman es*****@acm-dot-org.invalid
Eric Sosman schrieb:
priyanka wrote:
>I had a question. Is there any way of testing a string value in a switch statement. [...]
This is Question 20.17 in the comp.lang.c Frequently
Asked Questions (FAQ) list
http://c-faq.com/
@OP: Heed the above.
If you have a high number of strings or a high number of queries
requiring the switch, you can speed up the whole thing in a simple
manner under certain preconditions:
If the strings' first elements are spread out nicely over a part
of the alphabet, one can switch over s[0] and dispatch to a check
of the respective subsets.
Your "U of ...." does not lend itself to this approach.
For frequent queries over a very high number of strings, "real"
hashing may be better -- but this is outside the scope of
comp.lang.c.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
priyanka wrote:
switch(nameofIn stitution){
case UofCanada: printf( " U of canada\n");
break;
case UofIndia: printf( " U of India\n");
break;
char *string[] = {" U of canada\n", " U of India\n"};
printf(string[nameofInstituti on]);
--
pete
priyanka wrote:
Hi there,
I had a question. Is there any way of testing a string value in a
switch statement. I have about 50 string values that can be in a string
variable. I tried cheking them with the if else statements but it
looked pretty ugly and the string values to be tested is still
increasing. The switch statement seems to be a better choice then the
if else statement. But it seems that the switch statement accepts
integer values as the test condition. Can anybosy help me here or give
some idea ?
For eg,
switch(nameofIn stitution){
case UofCanada: printf( " U of canada\n");
break;
case UofIndia: printf( " U of India\n");
break;
............... ...
............... .
............... .....
about 50 such cases
default: printf(" Sorry, this university is not
listed\n");
die();
break;
}
Thank you,
Priya
Decide which language you are writing in.
You posted this on comp.lang.c++ as well.
One reason for deciding is that C++ has the
std::map which is very nice for your issue,
but you will have to implement your own map
or associative array in C.
--
Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq: http://www.comeaucomputing.com/learn/faq/
Other sites: http://www.josuttis.com -- C++ STL Library book http://www.sgi.com/tech/stl -- Standard Template Library
pete wrote:
priyanka wrote:
switch(nameofIn stitution){
case UofCanada: printf( " U of canada\n");
break;
case UofIndia: printf( " U of India\n");
break;
char *string[] = {" U of canada\n", " U of India\n"};
printf(string[nameofInstituti on]);
This won't work if any university is named:
"U of %foo".
printf("%s\n", string[name]) is safer, and it
takes the '\n' out of the names.
On Sun, 13 Aug 2006 19:30:22 UTC, "priyanka" <pr**********@g mail.com>
wrote:
Hi there,
I had a question. Is there any way of testing a string value in a
switch statement. I have about 50 string values that can be in a string
variable. I tried cheking them with the if else statements but it
looked pretty ugly and the string values to be tested is still
increasing. The switch statement seems to be a better choice then the
if else statement. But it seems that the switch statement accepts
integer values as the test condition. Can anybosy help me here or give
some idea ?
For eg,
switch(nameofIn stitution){
case UofCanada: printf( " U of canada\n");
break;
case UofIndia: printf( " U of India\n");
break;
Won't work.
If the functiononality you have to call for different strings are
really different you would do the following:
struct s_array {
char *s; /* string to compare */
pfnc pf; /* do work for that name */
....... /* some parameters related to that string */
};
int func(struct *a_array arr);
typedef (*pfnc)(struct *a_array);
int fnfoo(struct *a_array);
int fnbar(struct *a_array);
......
struct s_array array[] = {
{ "foo", fnfoo, ... },
{ "bar", fnbar, ... },
......
{ NULL }
};
......
struct a_array *p;
for (p = array; p->a; p++) {
if (!strcmp(cmpstr , p->a)) {
return p->pf(p);
}
}
return ERROR_NO_FUNC_F OUND;
The ... are for you to fill up. Get the idea and extend it to your
best usage.
--
Tschau/Bye
Herbert
Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 for '5'. So in
his switch statement, he omits a case for '5':
|
by: Scott C. Reynolds |
last post by:
In VB6 you could do a SELECT CASE that would evaluate each case for
truth and execute those statements, such as:
SELECT CASE True
case x > y:
dosomestuff()
case x = 5:
dosomestuff()
case y > x:
dosomestuff()
|
by: Juan |
last post by:
Thanks,
Juan.
|
by: OutdoorGuy |
last post by:
Greetings,
I was wondering if it is possible to test for a range of values in a
"Switch" statement? I have the following code, but it is generating an
error when I attempt to compile it. Any suggestions?
Thanks in advance!
public static String getMessageSwitch(int score)
{
|
by: priyanka |
last post by:
Hi there,
I had a question. Is there any way of testing a string value in a
switch statement. I have about 50 string values that can be in a string
variable. I tried cheking them with the if else statements but it
looked pretty ugly and the string values to be tested is still
increasing. The switch statement seems to be a better choice then the
if else statement. But it seems that the switch statement accepts
integer values as the test...
| |
by: Phuff |
last post by:
Hey all, I need some direction help. I have a switch case statement
that is seemingly my only option right now, but its too large and not
easy to maintain the code. Here goes...
I have part descriptions (ie. 3/8" X ____" NYLON ALL-THREAD RODS...or
____" x ____" X ____" ____ WRAPPED MULLION) that I need to replace the
blank lines on. I do a switch on the part id.
I have 53 in all out of the parts list. What fills the blank is...
|
by: Peter Kirk |
last post by:
Hi
i have a string variable which can take one of a set of many values.
Depending on the value I need to take different (but related) actions. So I
have a big if/else-if construct - but what is a better way of doing this?
if ("control" == commandString)
{
}
else if ("execute" == commandString)
|
by: Drowningwater |
last post by:
I'm writing a program and I've made a switch statement. I have several quesions:
1. I have a string variable and when I try to use that string variable with the switch function I get a compiling error that says: switch quantity not an integer
How to I get my switch statement to allow a string variable?
2. Can I make it accept 2 variables (i.e. If i wanted the person to type "go here" each word going into a seperate string variable)? ......
|
by: aaron80v |
last post by:
Hi,
A simple question: In ANSI-C, how to specify a string as the expression
for switch statement.
eg
switch (mystr) {
case "ABC" : bleh...
break;
|
by: Phillip B Oldham |
last post by:
What would be the optimal/pythonic way to subject an object to a
number of tests (based on the object's attributes) and redirect
program flow?
Say I had the following:
pets = {'name': 'fluffy', 'species': 'cat', 'size': 'small'}
pets = {'name': 'bruno', 'species': 'snake', 'size': 'small'}
pets = {'name': 'rex', 'species': 'dog', 'size': 'large'}
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |