How do you compare string lexicographically in c+ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do you compare string lexicographically in c+

like how do you compare "hello" to "HeLlo" or "helLl"

8th Apr 2022, 2:46 PM
Dhanraj Tamang
Dhanraj Tamang - avatar
1 Answer
+ 1
//lexiographically means comparing each letter of first string with corresponding letter in second string, until unequals. #include <iostream> using namespace std; int main() { string s1="hello",s2 = "Hello"; cout<<("hello"<"hellz")<<endl; // //by using compare method cout<<(s1>s2)<<endl; int i = s1.compare(s2); if(i==0) cout<<"both same"; if(i<0) cout<<s1 <<" is less"; else cout<<s2<<" is less"; return 0; } //may be many otherways by better inbuilt functions..
8th Apr 2022, 3:02 PM
Jayakrishna 🇮🇳