14

I want to compare two strings. Is it possible with strcmp? (I tried and it does not seem to work). Is string::compare a solution?

Other than this, is there a way to compare a string to a char?


Thanks for the early comments. I was coding in C++ and yes it was std::string like some of you mentioned. I didn't post the code because I wanted to learn the general knowledge and it is a pretty long code, so it was irrelevant for the question.

I think I learned the difference between C++ and C, thanks for pointing that out. And I will try to use overloaded operators now. And by the way string::compare worked too.

Eiko
  • 25,601
  • 15
  • 56
  • 71
Perex19
  • 324
  • 1
  • 4
  • 15
  • "seems like not working" - can you be more specific? What are the parameters and what is the result? – Mark Ransom Mar 30 '11 at 21:15
  • If you're programming in C++ then you should use C++ idioms, which means C++ `string` and its associated methods. For C use C idioms, such as `char *` and `strcpy`. – Paul R Mar 30 '11 at 21:16
  • what do you mean by "seems like not working"? – helloworld922 Mar 30 '11 at 21:16
  • "I tried and seems like not working" => post the code you used, the results you expected and what you got. And also you should specify what you mean with "string" (C-style ASCIIZ string? `std::string`? Something else?) and what you mean with "compare a string to a char" (it makes no sense, it's like comparing apples to oranges). – Matteo Italia Mar 30 '11 at 21:18
  • @Matteo Italia, It doesnt worked because compiler said it should be char* not string. And I asked "compare a string to a char" because I was pretty confused with string and chars. Now I learned that in C and C++ they are completely different. – Perex19 Mar 30 '11 at 21:27

6 Answers6

31

For C++, use std::string and compare using string::compare.

For C use strcmp. If your (i meant your programs) strings (for some weird reason) aren't nul terminated, use strncmp instead.

But why would someone not use something as simple as == for std::string ?

Sadique
  • 22,572
  • 7
  • 65
  • 91
  • 4
    Because `compare` has 3-way output `<0`, `0`, `>0`, whereas `==` has binary `true`/`false` output. Sometimes you need to take different action based on if the strings compare less or greater. A 3-way output lets you do that without comparing the strings twice. – Daniel Stevens Aug 13 '18 at 19:43
14

Assuming you mean std::string, why not use the overloaded operators: str1 == str2, str1 < str2?

Mark B
  • 95,107
  • 10
  • 109
  • 188
4

See std::basic_string::compare and std::basic_string operators reference (in particular, there exists operator==, operator!=, operator<, etc.). What else do you need?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • 1
    you could also betrying to figure out if a string is equal, greater, or less than another string all in one go (think which branch to descend down for a tree structure), in which case a strcmp like function is the more useful. – diverscuba23 Mar 30 '11 at 21:20
  • 1
    @diverscuba23 : Fair point indeed. :-] In which case, there is still `std::string::compare`, which should be preferred over `strcmp` in C++ for the reason @Paul R commented. I'll edit my post. – ildjarn Mar 30 '11 at 21:31
1

When using C++ use C++ functions viz. string::compare. When using C and you are forced to use char* for string, use strcmp

doron
  • 27,972
  • 12
  • 65
  • 103
1

By your question, "is there a way to compare a string to a char?" do you mean "How do I find out if a particular char is contained in a string?" If so, the the C-library function:

  char *strchr(const char *s, int c);

will do it for you.

-- pete

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51
1

std::string can contain (and compare!) embedded null characters.

are*comp(...) will compare c-style strings, comparing up to the first null character (or the specified max nr of bytes/characters)

string::compare is actually implemented as a template basic_string so you can expect it to work for other types such as wstring

On the unclear phrase to "compare a string to a char" you can compare the char to *string.begin() or lookup the first occurrence (string::find_first_of and string::find_first_not_of)

Disclaimer: typed on my HTC, typos reserved :)

sehe
  • 374,641
  • 47
  • 450
  • 633