comparin two strings in c++?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

comparin two strings in c++??

explaination

26th Dec 2016, 9:30 AM
Mayur Hiremath
Mayur Hiremath - avatar
7 Answers
+ 2
It's that easy: string str1 ("Easy"); string str2 ("Foo"); if (str1.compare(str2) != 0) // They are not equal if(str1.compare(str1) == 0) //Its the same so you use the compare method of the string class. It returns an integer. (0 if they equal).
26th Dec 2016, 9:42 AM
Oliver Wycisk
Oliver Wycisk - avatar
+ 1
AKCodingBlog, I do not agree with you. I think you can't compare them with "==" simply because there is no overloading of this operator in the class string
26th Dec 2016, 9:55 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
Oh no, I disagreed with the reason of why it is not possible, I did not say you said it was possible :) My mistake, I was ambiguous sorry
26th Dec 2016, 9:59 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
String class objects CAN be compared with '=='. http://www.cplusplus.com/reference/string/string/operators/
26th Dec 2016, 1:16 PM
Norbivar
Norbivar - avatar
0
A string is a char array. That's why you can not compare them with == Here is the specific documentation how to compare strings http://www.cplusplus.com/reference/string/string/compare/ ( GOOGLE is your friend )
26th Dec 2016, 9:43 AM
Andreas K
Andreas K - avatar
0
@Baptiste E. Prunier Please show me where I said "you can compare strings with ==" ?!
26th Dec 2016, 9:57 AM
Andreas K
Andreas K - avatar
0
No problem, I am sorry too. I think the way how strings are constructed results in your argumentation about the missing overloading. Can you agree with this?
26th Dec 2016, 10:01 AM
Andreas K
Andreas K - avatar