3

I know there is the strcmp but it just let me compare two strings, And I need to compare a lot of them

This one doesn't work:

if(strcmp (resposta, "S" || "s" || "N" || "n")== 0)
        printf("Resposta = S");
    else
        printf("Resposta != S");

    printf("\nfim");
Dannark
  • 716
  • 6
  • 12
  • You should write `if(strcmp(resposta, "S") == 0 || strcmp(resposta, "s") == 0 || ...)` – Brandin Mar 23 '14 at 16:04
  • answers suggesting `strcmp(str, "char1") || strcmp(str, "char2")...` won't work, as they will always evaluate to non-null (strings not equal). – EOF Mar 23 '14 at 16:11
  • 1
    For a case like this, just use a set (hash-based is probably best) and test if `resposta` is in a set. Set is not a feature of C, so you need to roll your own or use some existing library, or switch to a language which has a standard set. – hyde Mar 23 '14 at 16:15
  • I guess I'll do this program in another language :/ – Dannark Mar 23 '14 at 16:30
  • If you don't have some external reason to use C (like, small embedded system, existing code base, writing language-independent shared library, learning C...), then almost any other modern language will make life easier, for example by providing standard containers like set. But if you wish to use C, using a small extra library for this shouldn't be a big hurdle :-) – hyde Mar 23 '14 at 17:39

5 Answers5

5

Your method isn't working as you expected because the expression "S" || "s" || "N" || "n" is the same as "S" because of short circuit.

You have to compare it with the candidate strings one by one:

if (strcmp(resposta, "S") == 0
    || strcmp(resposta, "s") == 0
    || strcmp(resposta, "N") == 0
    || strcmp(resposta, "n") == 0)
{
    printf("Resposta = S");
}
CennoxX
  • 773
  • 1
  • 9
  • 20
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
2

If you're looking for a string that is only a single char, you can use a switch statement.

switch(*string) //will compare the first character of your string
{
    case('s'):
    {
        //whatever you do
        break;
    }
    case('S'):
    {
        //whatever else
        break;
    }
    ... //other characters
    default:
    {
        //error handling on incorrect input
        break;
    }
}

Edit: If you are comparing strings of different length (i.e. you are looking for a prefix in a string), be aware that strcmp() will never consider them equal.

If you need to find a prefix, you need to strncmp() (note the n) with each string and string length individually.

EOF
  • 6,273
  • 2
  • 26
  • 50
2
if( strcmp (resposta, "S") == 0 || strcmp (resposta,"s") == 0  || strcmp (resposta,"N") == 0 || strcmp (resposta, "n") == 0)
bitfiddler
  • 2,095
  • 1
  • 12
  • 11
2

The signature of the standard library function strcmp is -

int strcmp(const char *s1, const char *s2);

However, you are calling it as

strcmp(resposta, "S" || "s" || "N" || "n")

The second argument evaluates to 1, which is of type int, because the string literals evaluate to a pointer to its first character and it cannot be NULL. This is clearly wrong. You should replace it by

if(!((strcmp(resposta, "S") && strcmp(resposta, "N") && strcmp(resposta, "n")))
    printf("Resposta = S");
else
    printf("Resposta != S");
ajay
  • 9,402
  • 8
  • 44
  • 71
2

If you have a lot of strings to compare against, you could make an array out of them and iterate over it. In this example the array is terminated with a NULL (so the while condition works):

const char *strings[] = { "S", "Sim", "N", "NAO", NULL };
                                              // ^ add more strings here
const char **s = strings;
while (*s) {
    if (strcmp(resposta, *s) == 0) {
        (void) printf("Matched: %s\n", *s);
        break; // stop searching when a match is found (the matching string is *s)
    }
    ++s;
}
if (!*s) {
    (void) printf("Didn't match anything\n");
}

A better way if you have a lot of strings to match against would be to sort the array of strings and do binary search in it.

Arkku
  • 41,011
  • 10
  • 62
  • 84