Sign in to follow this  

[C++] Which string comes first?

This topic is 2794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive I have two std::strings: string 1 : "pa" string 2 : "ap" how do I make a program that compares the two strings and says that string "ap" comes first than "pa"? Another example: string 1: "pas" string 2: "pbs" String 1 comes first because "a" < "b".

Share this post


Link to post
Share on other sites
Also:

string1.compare(string2);

If the result is less than 0, string1 is lexicographically less than string2, and vice versa if the result is greater than 0. If the result is 0, they're equal.

Share this post


Link to post
Share on other sites
Depends on the type. In case of std::string, it's very easy:

if (string1 < string2) ...

If you're using C strings a.k.a char*, you can use:

if (strcmp(string1, string2) < 0) ...

Share this post


Link to post
Share on other sites

This topic is 2794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this