Re: switch statement with the string being tested



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!
.



Relevant Pages

  • Re: Lots of design issues needed.
    ... One possibility would be to replace the switch statement with an array ... > int showMenu() ... > int binarySearch(VPerson a, const string& x, const compare comp) const ...
    (alt.comp.lang.learn.c-cpp)
  • Re: switch statement with the string being tested
    ... I have about 50 string values that can be in a string ... The switch statement seems to be a better choice then the ... switch(idx = select_str(test)) { ...
    (comp.lang.c)
  • Re: member references
    ... then the next string would be the member name and sub member names etc. ... then use reflection to set/get the value. ... If you have a situation in which you think a switch statement does make sense, then rather than a plain index, you should probably use an enumeration. ... Why not just post a code sample that illustrates the basic idea. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help needed in solving C-errors in Linux (gcc)
    ... > When i tried to compile this test driver for an actual conversion library ... "multi-character character constant". ... you can't use a string in a switch statement. ...
    (comp.os.linux.development.apps)
  • Re: how to improve perf. ?
    ... Put a ^ at the beginning to specify match at the beginning of the string. ... > work differently depending of the 5 first chars of it. ... > cause those first chars order doesn't matter, it can't be found in the ...
    (comp.lang.php)