473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

switch statement with the string being tested

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
7 22098

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
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...
11
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 >...
4
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...
4
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...
5
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...
11
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...
3
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...
21
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;
2
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':...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.