switch statement with the string being tested

P

priyanka

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
 
B

Bill Pursell

priyanka said:
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))
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;
}
 
M

Michael Mair

Eric said:
priyanka said:
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
 
P

pete

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]);
 
T

Thomas Matthews

priyanka said:
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
 
B

Bill Pursell

pete said:
priyanka said:
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.
 
H

Herbert Rosenau

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!
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top