Re: switch statement with the string being tested
- From: "Herbert Rosenau" <os2guy@xxxxxxxxxxxxx>
- Date: Mon, 14 Aug 2006 21:07:19 +0000 (UTC)
On Sun, 13 Aug 2006 19:30:22 UTC, "priyanka" <priyankabhar@xxxxxxxxx>
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!
.
- References:
- switch statement with the string being tested
- From: priyanka
- switch statement with the string being tested
- Prev by Date: Re: string2float
- Next by Date: Re: program confusion
- Previous by thread: Re: switch statement with the string being tested
- Next by thread: program confusion
- Index(es):
Relevant Pages
|