এখানে আমরা দেখব C++-এ দুটি স্ট্রিং কীভাবে তুলনা করা যায়। C++-এ string ক্লাস আছে। সেটিতে compare() ফাংশন আছে। কিন্তু এখানে আমরা কন্ডিশনাল অপারেটর যেমন ==, !=, <, >, <=, >= ব্যবহার করব। এই অপারেটরগুলো স্ট্রিংয়ের ক্যারেক্টার একে একে চেক করে। ভালো বুঝতে কোডটি দেখুন।
উদাহরণ
#include<iostream> using namespace std; void compareStrings(string s1, string s2) { if (s1 != s2) cout << s1 << " is not equal to "<< s2 << endl; else if(s1 == s2) cout << "Strings are equal"; if (s1 > s2) cout << s1 << " is greater than "<< s2 << endl; else if(s1 < s2) cout << s2 << " is greater than "<< s1 << endl; } int main() { string s1("hello"); string s2("helLo"); compareStrings(s1, s2); }আউটপুট
hello is not equal to helLo hello is greater than helLo