Javatpoint Logo
Javatpoint Logo

STRCMP() IN C++

The strcmp() function in c++ is lexicographically comparing null terminating strings or two character arrays. It is a built-in library function in c++. As a parameter, the two-character array is taken by this function that is to be compared. According to the first dissimilar character, an integer value is returned by this strcmp() function in c++ among the two strings.

What is STRCMP() function in c++?

It is a built-in library function in c++. In the header file of <cstring>, this function is defined and prototyped. Arguments are taken as strings in this function, and to check the equality, it matches the strings. In both strings, the character is matched by this function for every index, so for that reason, the lexicographic function matches the strings. Until any similar or null type character is found, the comparison is done from the early first character of the strings and goes till for every character.

Syntax of the strcmpfunction in c++:

Using the strcmp function in c++, the syntax for this function is given below:

in this syntax, the str1 and str2 are the string names accordingly.

Parameter used in the strcmp function in c++:

The strcmp() function utilizes two parameters, such as str1 and str2, for comparing the pointers to the character arrays.

Value returned by the strcmp function in c++:

An integer value is returned by the strcmp function in c++. According to the first dissimilar character in c++, the value is calculated among the two strings. Three types of return values are generated by the strcmp function in c++.

1) Zero( 0 ):

Here zero is the return value in this function. In this case, both strings are the same. On the other hand, there is an equivalent index in both strings.

Example:

Output:

equal String
strcmp() return value is: 0

2) greater than zero ( >0 ):

If on the left-hand side of the string, the ASCII value of the first dissimilar character is greater than the corresponding character in the right-hand side string, then the value returned by it is greater than zero. The difference between the ASCII values of the first dissimilar character of the strings is the resultant value in it.

Example:

Output:

unequal String
strcmp() return value is: 32

Explanation:

In the above code, two strings, str1 and str2, are declared and initialized, respectively. They are then passed through strcmp() function. The function's return value gets stored in the variable returnvalue. The first unmatched character in the strings is found at index 0, where characters in both the strings. The ASCII values of both characters are 115 and 83, respectively. Hence, the difference in the ASCII value turns out as 32. A conditional statement is used to check whether strings are the same by verifying whether returnvalue is equal to zero.

3) less than zero ( <0 ):

If the ASCII value on the left-hand side of the first dissimilar character of the string is less than the right-hand side string of the corresponding character, then the return value will be less than zero. The difference between the ASCII values of the first dissimilar character of the strings, i.e., str1 -str2, is called the resultant value of it.

Example:

Output:

unequal String
strcmp() return value is: -32

Prototype of the strcmp():

In c++, the strcmp() prototype is described in the header file called cstring:

the elements of the strings like str1 and str2 should be lexicographically matched with strcmp().

The difference between the first dissimilar pairs of the characters which occur in str1 and str2 is called the return value.

Undefined behavior of Strcmp():

In C++, if one of the parameters will not point to null-terminated strings or c++ character arrays, the function strcmp shows exceptions or undefined behavior.

Example:

Compilation error:

Output:

no output

Using user-defined function strcmp function implementation:

Output:

equal strings
strcmp() return value is:  0

Conclusion:

  • In c++, the strcmp function is a built-in library function.
  • It is defined in the header file like 'string.h', and it is prototyped.
  • For comparison, this function takes two character arrays as parameters.
  • An integer value is returned by this function in c++, which calculates according to the first dissimilar character among two strings.
  • In c++, this function shows undefined behavior if one of the parameters will not point to a c character array or null terminating strings.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA