String Compare In Dev C++

This came up in another thread. I gave some advice that I'm not longer sure of. Rather than hijacking that thread, I figured I'd start my own. I advised against using the in that thread. The context was this:

  1. String Compare C
  2. C++ How To Compare Strings
  3. Strings In Dev C++
  4. C# String Comparison
  5. C++ Compare String To Char

String Compare C

The following example demonstrates that the Compare(String, String, Boolean) method is equivalent to using ToUpper or ToLower when comparing strings. Using namespace System; void main // Create upper-case characters from their Unicode code units. Jun 28, 2017  std::string::compare in C compare is a public member function of string class. It compares the value of the string object (or a substring) to the sequence of.

Ancient Dragon said the above was fine and that compare was unnecessary. So I wondered if maybe was only bad when comparing two string variables, so I did a little test program and it worked:

The line displayed to the screen. So my question is when is it bad to use when comparing strings? I thought the program above was not supposed to work. Apparently I was mistaken. If it's always fine to use , do we ever need to use the 'compare' function?

C++
  • 4 Contributors
  • forum 11 Replies
  • 760 Views
  • 2 Days Discussion Span
  • commentLatest Postby VernonDozierLatest Post

Recommended Answers

C++ How To Compare Strings

Heres the same basic information using a slightly different explanation:

Given:
string a = 'hello';

a is not an address. The STL string object may have within it a char (to be used as a C style string) as a member variable, in addition to other member variables such …

Strings In Dev C++

Jump to Post

All 11 Replies

Ancient Dragon5,243

>>string subChoice =';
It isn't necessary to provide an initializer for strings because that's the default. Just string subChoice; is sufficient.

>> do we ever need to use the 'compare' function
You use it when you need to know if one string is less than, equal to, or greater than the second string, such as in sorting algorithms. You could also use the < and > operators but then that might be too slow when used in if conditions because the comparison would have to be repeated.

Hello! :)

I'm having a problem while comparing one character from a string with a ... string.

This is a part of a pretty complex loop (or, it's complex for me, since I'm new to C++ and programming), but the case is that I need to compare the value of a given i index of a string to a string. For example:

Well, this is just a silly example program, but I hope you see whats wrong (because I don't...)

Thanks! :)

(PS: If you don't understand my english or if I have done something against the forum rules, please send me a PM, so it won't happen again.)

  • 3 Contributors
  • forum 3 Replies
  • 9,356 Views
  • 6 Hours Discussion Span
  • commentLatest Postby Arne KristofferLatest Post

Nick Evan4,005

Very simple mistake: if (str[x] 'a') should be: if (str[x] 'a') You are comparing each character from a string with another character, so you need single quotes instead of double

if I have done something against the forum rules

Actually, you are one of the few people who get the Code-tags right in their first post, so : bravo!

C# String Comparison

Dev

C++ Compare String To Char

Edited by Nick Evan: n/a