यहां हम देखेंगे कि C++ में दो स्ट्रिंग्स की तुलना कैसे करें। सी ++ में स्ट्रिंग क्लास है। इसमें स्ट्रिंग्स की तुलना करने के लिए मानक पुस्तकालय में तुलना () फ़ंक्शन भी है। यह फ़ंक्शन स्ट्रिंग वर्णों को एक-एक करके जांचता है, यदि कुछ बेमेल हैं, तो यह गैर-शून्य मान देता है। आइए बेहतर विचार प्राप्त करने के लिए कोड देखें।
उदाहरण
#include<iostream>
using namespace std;
void compareStrings(string s1, string s2) {
int compare = s1.compare(s2);
if (compare != 0)
cout << s1 << " is not equal to "<< s2 << endl;
else if(compare == 0)
cout << "Strings are equal";
if (compare > 0)
cout << s1 << " is greater than "<< s2 << " difference is: " << compare << endl;
else if(compare < 0)
cout << s2 << " is greater than "<< s1 << " difference is: " << compare << endl;
}
int main() {
string s1("hello");
string s2("helLo");
compareStrings(s1, s2);
} आउटपुट
hello is not equal to helLo hello is greater than helLo difference is: 1