What is the best way to compare multiple strings in C? [closed]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to find the best way to compare multiple strings in C.
Currently, I'm using strcmp();
function, but it's turning out to be too many if
statements. I was also using ternary operator but unfortunately for me, it doesn't help.
Is there any better solution?
Here is example code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char command = "First Second Third";
char * del = " ";
char * token;
char * strings[3];
int n = 0;
token = strtok(command, del);
while (token != NULL){
strings[n] = token;
token = strtok(NULL, del);
n++;
}
// Now, strings[0] = "First", strings[1] = "Second", and strings[2] = "Third"
// Only examine strings[1] and strings[2] after we know strings[0] = "First".
if (strcmp("First", strings[0]) == 0){
//do something
if (strcmp("Second", strings[1]) == 0){
//do something
if (strcmp("Third", strings[2]) == 0){
//do something
printf("CORRECT");
//do something
}
}
}
return 0;
}
c string strcmp
closed as too broad by Yunnosch, melpomene, Jean-François Fabre♦, pushkin, Matt Clark Nov 26 '18 at 22:18
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I'm trying to find the best way to compare multiple strings in C.
Currently, I'm using strcmp();
function, but it's turning out to be too many if
statements. I was also using ternary operator but unfortunately for me, it doesn't help.
Is there any better solution?
Here is example code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char command = "First Second Third";
char * del = " ";
char * token;
char * strings[3];
int n = 0;
token = strtok(command, del);
while (token != NULL){
strings[n] = token;
token = strtok(NULL, del);
n++;
}
// Now, strings[0] = "First", strings[1] = "Second", and strings[2] = "Third"
// Only examine strings[1] and strings[2] after we know strings[0] = "First".
if (strcmp("First", strings[0]) == 0){
//do something
if (strcmp("Second", strings[1]) == 0){
//do something
if (strcmp("Third", strings[2]) == 0){
//do something
printf("CORRECT");
//do something
}
}
}
return 0;
}
c string strcmp
closed as too broad by Yunnosch, melpomene, Jean-François Fabre♦, pushkin, Matt Clark Nov 26 '18 at 22:18
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Show us your code.
– Robert Harvey♦
Nov 26 '18 at 20:33
1
Please focus your question by showing code which demonstrates your actual problem or the specific case you want to optimise.
– Yunnosch
Nov 26 '18 at 20:34
sometimes you just have to type a lot of code - dont fish for tricks that obscure what you are trying to do
– pm100
Nov 26 '18 at 21:27
This sounds like an X/Y problem - why do you want to match multiple strings? What are you trying to accomplish by matching multiple strings? What is the high-level description of what you are trying to do?
– John Bode
Nov 26 '18 at 21:36
add a comment |
I'm trying to find the best way to compare multiple strings in C.
Currently, I'm using strcmp();
function, but it's turning out to be too many if
statements. I was also using ternary operator but unfortunately for me, it doesn't help.
Is there any better solution?
Here is example code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char command = "First Second Third";
char * del = " ";
char * token;
char * strings[3];
int n = 0;
token = strtok(command, del);
while (token != NULL){
strings[n] = token;
token = strtok(NULL, del);
n++;
}
// Now, strings[0] = "First", strings[1] = "Second", and strings[2] = "Third"
// Only examine strings[1] and strings[2] after we know strings[0] = "First".
if (strcmp("First", strings[0]) == 0){
//do something
if (strcmp("Second", strings[1]) == 0){
//do something
if (strcmp("Third", strings[2]) == 0){
//do something
printf("CORRECT");
//do something
}
}
}
return 0;
}
c string strcmp
I'm trying to find the best way to compare multiple strings in C.
Currently, I'm using strcmp();
function, but it's turning out to be too many if
statements. I was also using ternary operator but unfortunately for me, it doesn't help.
Is there any better solution?
Here is example code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char command = "First Second Third";
char * del = " ";
char * token;
char * strings[3];
int n = 0;
token = strtok(command, del);
while (token != NULL){
strings[n] = token;
token = strtok(NULL, del);
n++;
}
// Now, strings[0] = "First", strings[1] = "Second", and strings[2] = "Third"
// Only examine strings[1] and strings[2] after we know strings[0] = "First".
if (strcmp("First", strings[0]) == 0){
//do something
if (strcmp("Second", strings[1]) == 0){
//do something
if (strcmp("Third", strings[2]) == 0){
//do something
printf("CORRECT");
//do something
}
}
}
return 0;
}
c string strcmp
c string strcmp
edited Nov 27 '18 at 17:48
donjuedo
1,9001018
1,9001018
asked Nov 26 '18 at 20:32
SansaraSansara
73
73
closed as too broad by Yunnosch, melpomene, Jean-François Fabre♦, pushkin, Matt Clark Nov 26 '18 at 22:18
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Yunnosch, melpomene, Jean-François Fabre♦, pushkin, Matt Clark Nov 26 '18 at 22:18
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Show us your code.
– Robert Harvey♦
Nov 26 '18 at 20:33
1
Please focus your question by showing code which demonstrates your actual problem or the specific case you want to optimise.
– Yunnosch
Nov 26 '18 at 20:34
sometimes you just have to type a lot of code - dont fish for tricks that obscure what you are trying to do
– pm100
Nov 26 '18 at 21:27
This sounds like an X/Y problem - why do you want to match multiple strings? What are you trying to accomplish by matching multiple strings? What is the high-level description of what you are trying to do?
– John Bode
Nov 26 '18 at 21:36
add a comment |
Show us your code.
– Robert Harvey♦
Nov 26 '18 at 20:33
1
Please focus your question by showing code which demonstrates your actual problem or the specific case you want to optimise.
– Yunnosch
Nov 26 '18 at 20:34
sometimes you just have to type a lot of code - dont fish for tricks that obscure what you are trying to do
– pm100
Nov 26 '18 at 21:27
This sounds like an X/Y problem - why do you want to match multiple strings? What are you trying to accomplish by matching multiple strings? What is the high-level description of what you are trying to do?
– John Bode
Nov 26 '18 at 21:36
Show us your code.
– Robert Harvey♦
Nov 26 '18 at 20:33
Show us your code.
– Robert Harvey♦
Nov 26 '18 at 20:33
1
1
Please focus your question by showing code which demonstrates your actual problem or the specific case you want to optimise.
– Yunnosch
Nov 26 '18 at 20:34
Please focus your question by showing code which demonstrates your actual problem or the specific case you want to optimise.
– Yunnosch
Nov 26 '18 at 20:34
sometimes you just have to type a lot of code - dont fish for tricks that obscure what you are trying to do
– pm100
Nov 26 '18 at 21:27
sometimes you just have to type a lot of code - dont fish for tricks that obscure what you are trying to do
– pm100
Nov 26 '18 at 21:27
This sounds like an X/Y problem - why do you want to match multiple strings? What are you trying to accomplish by matching multiple strings? What is the high-level description of what you are trying to do?
– John Bode
Nov 26 '18 at 21:36
This sounds like an X/Y problem - why do you want to match multiple strings? What are you trying to accomplish by matching multiple strings? What is the high-level description of what you are trying to do?
– John Bode
Nov 26 '18 at 21:36
add a comment |
2 Answers
2
active
oldest
votes
OP's code has some problems
while (token != NULL)
has no limit to 3 loops. Code may attemptstrings[3] = token;
// while (token != NULL){
while (n < 3 && token != NULL){
Code uses
strings[0]
,strings[1]
,strings[2]
without first insuring that many tokens were parsed.
// strcmp("First", strings[0]) == 0
(n > 0 && strcmp("First", strings[0]) == 0)
Code saves a pointer to the original string. Once
strtok()
is call again, the prior token can be lost/changed.
The "best" way involves hashing the key and targets, yet that is a lot to explain here.
Alternative: With such a simple match needed as in OP's example, code could use "%n"
to record the offset of the scan.
int n1 = 0, n2 = 0, n3 = 0;
sscanf(command, "First %n Second %n Third %n", &n1, &n2, &n3);
if (n1) { // n1 is non-zero if scan matched "First"
//do something
if (n2) { // n2 is non-zero if scan matched "First Second"
//do something
if (n3) {
//do something
printf("CORRECT");
//do something
}
}
}
add a comment |
It looks like your if
statements should be using strcmp()
instead of the posted strtok()
. Also, I think you are looking for something more like if ... else if ... else if ...
, rather than nested if's
.
Depending on the real program you're writing (vs. posted), you could also use a switch()
based on the first character in the string to compare. It amounts to a crude hashing function. (you may want to learn more about hashing to adapt what you have to what you want).
EDIT:
Maybe this is what you're getting at:
.
.
.
if (strcmp(strings[0], "cmdA") == 0)
processCmdA(strings[1], strings[2]);
else
if (strcmp(strings[0], "cmdB") == 0)
processCmdB(strings[1], strings[2]);
else
if (strcmp(strings[0], "cmdC") == 0)
processCmdC(strings[1], strings[2]);
.
.
.
void processCmdA(char* optionA, char* inputFileName)
{
if (strcmp(optionA, "parse") == 0)
parseFile(inputFileName);
else
if (strcmp(optionA, "cksum") == 0)
computeCheckSum(inputFileName);
else
.
.
.
}
void parseFile(char* inputFileName)
{
// do parse stuff
}
.
.
.
Sorry for the mistake with strtok function inif
statements. I edited the post. What I want to do is check if the command is correct. The parts of it can vary for examplechar command = "First Second Third";
orchar command = "foo bar";
– Sansara
Nov 26 '18 at 21:27
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
OP's code has some problems
while (token != NULL)
has no limit to 3 loops. Code may attemptstrings[3] = token;
// while (token != NULL){
while (n < 3 && token != NULL){
Code uses
strings[0]
,strings[1]
,strings[2]
without first insuring that many tokens were parsed.
// strcmp("First", strings[0]) == 0
(n > 0 && strcmp("First", strings[0]) == 0)
Code saves a pointer to the original string. Once
strtok()
is call again, the prior token can be lost/changed.
The "best" way involves hashing the key and targets, yet that is a lot to explain here.
Alternative: With such a simple match needed as in OP's example, code could use "%n"
to record the offset of the scan.
int n1 = 0, n2 = 0, n3 = 0;
sscanf(command, "First %n Second %n Third %n", &n1, &n2, &n3);
if (n1) { // n1 is non-zero if scan matched "First"
//do something
if (n2) { // n2 is non-zero if scan matched "First Second"
//do something
if (n3) {
//do something
printf("CORRECT");
//do something
}
}
}
add a comment |
OP's code has some problems
while (token != NULL)
has no limit to 3 loops. Code may attemptstrings[3] = token;
// while (token != NULL){
while (n < 3 && token != NULL){
Code uses
strings[0]
,strings[1]
,strings[2]
without first insuring that many tokens were parsed.
// strcmp("First", strings[0]) == 0
(n > 0 && strcmp("First", strings[0]) == 0)
Code saves a pointer to the original string. Once
strtok()
is call again, the prior token can be lost/changed.
The "best" way involves hashing the key and targets, yet that is a lot to explain here.
Alternative: With such a simple match needed as in OP's example, code could use "%n"
to record the offset of the scan.
int n1 = 0, n2 = 0, n3 = 0;
sscanf(command, "First %n Second %n Third %n", &n1, &n2, &n3);
if (n1) { // n1 is non-zero if scan matched "First"
//do something
if (n2) { // n2 is non-zero if scan matched "First Second"
//do something
if (n3) {
//do something
printf("CORRECT");
//do something
}
}
}
add a comment |
OP's code has some problems
while (token != NULL)
has no limit to 3 loops. Code may attemptstrings[3] = token;
// while (token != NULL){
while (n < 3 && token != NULL){
Code uses
strings[0]
,strings[1]
,strings[2]
without first insuring that many tokens were parsed.
// strcmp("First", strings[0]) == 0
(n > 0 && strcmp("First", strings[0]) == 0)
Code saves a pointer to the original string. Once
strtok()
is call again, the prior token can be lost/changed.
The "best" way involves hashing the key and targets, yet that is a lot to explain here.
Alternative: With such a simple match needed as in OP's example, code could use "%n"
to record the offset of the scan.
int n1 = 0, n2 = 0, n3 = 0;
sscanf(command, "First %n Second %n Third %n", &n1, &n2, &n3);
if (n1) { // n1 is non-zero if scan matched "First"
//do something
if (n2) { // n2 is non-zero if scan matched "First Second"
//do something
if (n3) {
//do something
printf("CORRECT");
//do something
}
}
}
OP's code has some problems
while (token != NULL)
has no limit to 3 loops. Code may attemptstrings[3] = token;
// while (token != NULL){
while (n < 3 && token != NULL){
Code uses
strings[0]
,strings[1]
,strings[2]
without first insuring that many tokens were parsed.
// strcmp("First", strings[0]) == 0
(n > 0 && strcmp("First", strings[0]) == 0)
Code saves a pointer to the original string. Once
strtok()
is call again, the prior token can be lost/changed.
The "best" way involves hashing the key and targets, yet that is a lot to explain here.
Alternative: With such a simple match needed as in OP's example, code could use "%n"
to record the offset of the scan.
int n1 = 0, n2 = 0, n3 = 0;
sscanf(command, "First %n Second %n Third %n", &n1, &n2, &n3);
if (n1) { // n1 is non-zero if scan matched "First"
//do something
if (n2) { // n2 is non-zero if scan matched "First Second"
//do something
if (n3) {
//do something
printf("CORRECT");
//do something
}
}
}
edited Nov 26 '18 at 21:37
answered Nov 26 '18 at 21:29
chuxchux
85.1k874157
85.1k874157
add a comment |
add a comment |
It looks like your if
statements should be using strcmp()
instead of the posted strtok()
. Also, I think you are looking for something more like if ... else if ... else if ...
, rather than nested if's
.
Depending on the real program you're writing (vs. posted), you could also use a switch()
based on the first character in the string to compare. It amounts to a crude hashing function. (you may want to learn more about hashing to adapt what you have to what you want).
EDIT:
Maybe this is what you're getting at:
.
.
.
if (strcmp(strings[0], "cmdA") == 0)
processCmdA(strings[1], strings[2]);
else
if (strcmp(strings[0], "cmdB") == 0)
processCmdB(strings[1], strings[2]);
else
if (strcmp(strings[0], "cmdC") == 0)
processCmdC(strings[1], strings[2]);
.
.
.
void processCmdA(char* optionA, char* inputFileName)
{
if (strcmp(optionA, "parse") == 0)
parseFile(inputFileName);
else
if (strcmp(optionA, "cksum") == 0)
computeCheckSum(inputFileName);
else
.
.
.
}
void parseFile(char* inputFileName)
{
// do parse stuff
}
.
.
.
Sorry for the mistake with strtok function inif
statements. I edited the post. What I want to do is check if the command is correct. The parts of it can vary for examplechar command = "First Second Third";
orchar command = "foo bar";
– Sansara
Nov 26 '18 at 21:27
add a comment |
It looks like your if
statements should be using strcmp()
instead of the posted strtok()
. Also, I think you are looking for something more like if ... else if ... else if ...
, rather than nested if's
.
Depending on the real program you're writing (vs. posted), you could also use a switch()
based on the first character in the string to compare. It amounts to a crude hashing function. (you may want to learn more about hashing to adapt what you have to what you want).
EDIT:
Maybe this is what you're getting at:
.
.
.
if (strcmp(strings[0], "cmdA") == 0)
processCmdA(strings[1], strings[2]);
else
if (strcmp(strings[0], "cmdB") == 0)
processCmdB(strings[1], strings[2]);
else
if (strcmp(strings[0], "cmdC") == 0)
processCmdC(strings[1], strings[2]);
.
.
.
void processCmdA(char* optionA, char* inputFileName)
{
if (strcmp(optionA, "parse") == 0)
parseFile(inputFileName);
else
if (strcmp(optionA, "cksum") == 0)
computeCheckSum(inputFileName);
else
.
.
.
}
void parseFile(char* inputFileName)
{
// do parse stuff
}
.
.
.
Sorry for the mistake with strtok function inif
statements. I edited the post. What I want to do is check if the command is correct. The parts of it can vary for examplechar command = "First Second Third";
orchar command = "foo bar";
– Sansara
Nov 26 '18 at 21:27
add a comment |
It looks like your if
statements should be using strcmp()
instead of the posted strtok()
. Also, I think you are looking for something more like if ... else if ... else if ...
, rather than nested if's
.
Depending on the real program you're writing (vs. posted), you could also use a switch()
based on the first character in the string to compare. It amounts to a crude hashing function. (you may want to learn more about hashing to adapt what you have to what you want).
EDIT:
Maybe this is what you're getting at:
.
.
.
if (strcmp(strings[0], "cmdA") == 0)
processCmdA(strings[1], strings[2]);
else
if (strcmp(strings[0], "cmdB") == 0)
processCmdB(strings[1], strings[2]);
else
if (strcmp(strings[0], "cmdC") == 0)
processCmdC(strings[1], strings[2]);
.
.
.
void processCmdA(char* optionA, char* inputFileName)
{
if (strcmp(optionA, "parse") == 0)
parseFile(inputFileName);
else
if (strcmp(optionA, "cksum") == 0)
computeCheckSum(inputFileName);
else
.
.
.
}
void parseFile(char* inputFileName)
{
// do parse stuff
}
.
.
.
It looks like your if
statements should be using strcmp()
instead of the posted strtok()
. Also, I think you are looking for something more like if ... else if ... else if ...
, rather than nested if's
.
Depending on the real program you're writing (vs. posted), you could also use a switch()
based on the first character in the string to compare. It amounts to a crude hashing function. (you may want to learn more about hashing to adapt what you have to what you want).
EDIT:
Maybe this is what you're getting at:
.
.
.
if (strcmp(strings[0], "cmdA") == 0)
processCmdA(strings[1], strings[2]);
else
if (strcmp(strings[0], "cmdB") == 0)
processCmdB(strings[1], strings[2]);
else
if (strcmp(strings[0], "cmdC") == 0)
processCmdC(strings[1], strings[2]);
.
.
.
void processCmdA(char* optionA, char* inputFileName)
{
if (strcmp(optionA, "parse") == 0)
parseFile(inputFileName);
else
if (strcmp(optionA, "cksum") == 0)
computeCheckSum(inputFileName);
else
.
.
.
}
void parseFile(char* inputFileName)
{
// do parse stuff
}
.
.
.
edited Nov 27 '18 at 11:35
answered Nov 26 '18 at 21:14
donjuedodonjuedo
1,9001018
1,9001018
Sorry for the mistake with strtok function inif
statements. I edited the post. What I want to do is check if the command is correct. The parts of it can vary for examplechar command = "First Second Third";
orchar command = "foo bar";
– Sansara
Nov 26 '18 at 21:27
add a comment |
Sorry for the mistake with strtok function inif
statements. I edited the post. What I want to do is check if the command is correct. The parts of it can vary for examplechar command = "First Second Third";
orchar command = "foo bar";
– Sansara
Nov 26 '18 at 21:27
Sorry for the mistake with strtok function in
if
statements. I edited the post. What I want to do is check if the command is correct. The parts of it can vary for example char command = "First Second Third";
or char command = "foo bar";
– Sansara
Nov 26 '18 at 21:27
Sorry for the mistake with strtok function in
if
statements. I edited the post. What I want to do is check if the command is correct. The parts of it can vary for example char command = "First Second Third";
or char command = "foo bar";
– Sansara
Nov 26 '18 at 21:27
add a comment |
Show us your code.
– Robert Harvey♦
Nov 26 '18 at 20:33
1
Please focus your question by showing code which demonstrates your actual problem or the specific case you want to optimise.
– Yunnosch
Nov 26 '18 at 20:34
sometimes you just have to type a lot of code - dont fish for tricks that obscure what you are trying to do
– pm100
Nov 26 '18 at 21:27
This sounds like an X/Y problem - why do you want to match multiple strings? What are you trying to accomplish by matching multiple strings? What is the high-level description of what you are trying to do?
– John Bode
Nov 26 '18 at 21:36