এখানে আমরা দেখব C++-এ দুটি স্ট্রিং কীভাবে তুলনা করা যায়। C++-এ string ক্লাস আছে। সেটিতে স্ট্যান্ডার্ড লাইব্রেরির compare() ফাংশন আছে স্ট্রিং তুলনার জন্য। এই ফাংশন স্ট্রিংয়ের ক্যারেক্টারগুলো একে একে চেক করে, যদি মিসম্যাচ থাকে তবে নন-জিরো ভ্যালু রিটার্ন করে। ভালো বুঝতে কোডটি দেখুন।
উদাহরণ
#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