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

switch statement with the string being tested

P: n/a
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(nameofInstitution){
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

Aug 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

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(nameofInstitution){
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_strings[] = {
"string 0",
"string 1",
"string 2",
};

int
select_str(char *s)
{
int i;
for (i=0; i < sizeof accepted_strings/sizeof *accepted_strings;
i++)
if (!strcmp(s, accepted_strings[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("selected %d\n", idx);
break;
default:
printf("unknown string\n");
break;
}
return EXIT_SUCCESS;
}

Aug 13 '06 #2

P: n/a
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
Aug 13 '06 #3

P: n/a
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.
Aug 13 '06 #4

P: n/a
priyanka wrote:
switch(nameofInstitution){
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[nameofInstitution]);

--
pete
Aug 13 '06 #5

P: n/a
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(nameofInstitution){
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.learn.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

Aug 14 '06 #6

P: n/a

pete wrote:
priyanka wrote:
switch(nameofInstitution){
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[nameofInstitution]);
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.

Aug 14 '06 #7

P: n/a
On Sun, 13 Aug 2006 19:30:22 UTC, "priyanka" <pr**********@gmail.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(nameofInstitution){
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_FOUND;

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!
Aug 14 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.