28

I wanted to compare a string without actually defining one of them as a string, something like this,

if (string == "add")

Do I have to declare "add" as a string or is it possible to compare in a similar way?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Anon
  • 347
  • 2
  • 4
  • 8
  • 7
    What is the type of `string`? Is it a C++ `std::string` object, or simply a `const char*`? – e.James Jun 03 '11 at 03:40
  • @e.James awnser is good. Also worth mentioning that a string or char sequence literal should be enclosed with `"` (double quotes), single quotes are for single char literals. – Aleuck Nov 16 '17 at 03:16

4 Answers4

65

In C++ the std::string class implements the comparison operators, so you can perform the comparison using == just as you would expect:

if (string == "add") { ... }

When used properly, operator overloading is an excellent C++ feature.

WerWet
  • 323
  • 5
  • 14
e.James
  • 116,942
  • 41
  • 177
  • 214
  • Well now I'm *really* confused. If `string` is a `std::string` object, this will work, but then `strcmp()` would *not* have worked. Which is it? I'm dying to know! `:)` – e.James Jun 03 '11 at 03:49
  • 3
    But it should be noted, that the comparison will yield a different result if "string" is `char*` ! – Frunsi Jun 03 '11 at 03:57
  • 3
    e.James: if "string" is std::string, then `if( strcmp(string.c_str(),"add")==0 ) {...}` would work. – Frunsi Jun 03 '11 at 03:59
  • @frunsi: Yes, but that would be *really* unnecessary. I was referring to Anon's comment in Christopher Armstrong's answer (where he says that `if (strcmp(string, "add") == 0) { ... }` worked. – e.James Jun 03 '11 at 04:01
  • @frunsi: I am worried that `string` *is* a `char*`, in which case this is the wrong answer. I was hoping Anon would clarify that. – e.James Jun 03 '11 at 04:03
  • What if `string` is a `char*` ? What should I do? –  Mar 04 '14 at 14:49
8

You need to use strcmp.

if (strcmp(string,"add") == 0){
    print("success!");
}
Christopher Armstrong
  • 7,907
  • 2
  • 26
  • 28
  • 9
    Note: `strcmp()` is a C function which takes pointers to `const char*` as its arguments. It works in this case because the `std::string` object is converted back into a `const char*`, but this is *not* the best way to do things in C++ – e.James Jun 03 '11 at 03:31
  • 5
    @e.James: No, `std::string` does not implicitly convert to `const char*`, you need to call `s.c_str()` for that. But according to the question "without defining either one as a string", `string` isn't a `std::string` to begin with. – Ben Voigt Jun 03 '11 at 03:35
  • @Ben Voigt: Ah, yes you are right about the down-conversion. My mistake. – e.James Jun 03 '11 at 03:38
  • 2
    In C code you should prefer strncmp() over strcmp(), which is safer solution. – MBI Mar 12 '16 at 13:39
0

You could use strcmp():

/* strcmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char szKey[] = "apple";
  char szInput[80];
  do {
     printf ("Guess my favourite fruit? ");
     gets (szInput);
  } while (strcmp (szKey,szInput) != 0);
  puts ("Correct answer!");
  return 0;
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
Algorithmist
  • 6,657
  • 7
  • 35
  • 49
0

We use the following set of instructions in the C++ computer language.

Objective: verify if the value inside std::string container is equal to "add":

if (sString.compare(“add”) == 0) { //if equal
    // Execute
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
masarapmabuhay
  • 466
  • 1
  • 9
  • 15