Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison operators for C strings

I am having some difficulty locating information of comparing C strings. I understand that unlike C++, C does not support operator overloading, so I'm wondering if there is any way to check if one string is greater/less than another (e.g. str1 > str2)?

Thanks ahead of time for your responses. This is honestly one of the first times I have actually had to ask a question because I could not find a related post.

like image 308
user1149963 Avatar asked Jan 17 '12 07:01

user1149963


People also ask

Can I use == to compare strings in C?

In C, string values (including string literals) are represented as arrays of char followed by a 0 terminator, and you cannot use the == operator to compare array contents; the language simply doesn't define the operation.

How do you compare strings in C?

In order to compare two strings, we can use String's strcmp() function. The strcmp() function is a C library function used to compare two strings in a lexicographical manner. The function returns 0 if both the strings are equal or the same. The input string has to be a char array of C-style string.

Can you use comparison operators on strings?

The comparison operators also work on strings. To see if two strings are equal you simply write a boolean expression using the equality operator.

Can we use == operator for strings?

In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .


2 Answers

There are several, each serving different purposes (omitting wide character variants for now).

strcmp – compares two strings, character by character (with the C notion of what strings are equal or not – that doesn't need to coincide with how humans think – see strcoll). There's a variant for comparing only the first at most n characters, strncmp.

strcasecmp – compares two strings, ignoring case. There's a variant for comparing only the first at most n characters, strncasecmp.

strcoll – compares two strings, observing the currently set locale (which is why it's called collation, not comparing in this case). If you want ss and ß to compare equal for a German audience, then this is what you should use.


Where you might write

if (string1 > string2) ...

in a language, you have to write

if (strmp(string1, string2) > 0) ...

in C. Essentially you move both operands into the function call, retain the comparison operator and compare with 0 instead.

like image 106
Joey Avatar answered Oct 02 '22 12:10

Joey


Use strcmp() in C.

for example if you want to compare two strings s1 and s2 then,

strcmp(s1,s2) will return 0 if they are equal, positive integer if s1 is greater than s2 and negative integer if s1 is lesser than s2.

like image 24
CodeR Avatar answered Oct 01 '22 12:10

CodeR