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







-1















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









share|improve this 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


















-1















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









share|improve this 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














-1












-1








-1








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












2 Answers
2






active

oldest

votes


















1














OP's code has some problems





  • while (token != NULL) has no limit to 3 loops. Code may attempt strings[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
}
}
}





share|improve this answer

































    0














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





    share|improve this answer


























    • 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




















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    OP's code has some problems





    • while (token != NULL) has no limit to 3 loops. Code may attempt strings[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
    }
    }
    }





    share|improve this answer






























      1














      OP's code has some problems





      • while (token != NULL) has no limit to 3 loops. Code may attempt strings[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
      }
      }
      }





      share|improve this answer




























        1












        1








        1







        OP's code has some problems





        • while (token != NULL) has no limit to 3 loops. Code may attempt strings[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
        }
        }
        }





        share|improve this answer















        OP's code has some problems





        • while (token != NULL) has no limit to 3 loops. Code may attempt strings[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
        }
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 21:37

























        answered Nov 26 '18 at 21:29









        chuxchux

        85.1k874157




        85.1k874157

























            0














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





            share|improve this answer


























            • 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


















            0














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





            share|improve this answer


























            • 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
















            0












            0








            0







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





            share|improve this answer















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






            share|improve this answer














            share|improve this answer



            share|improve this answer








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



















            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





            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen