Strcmp In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Strcmp In C++

Strcmp In C++

BLUF: Mastering Strcmp In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Strcmp In C++

C++ is renowned for its efficiency. Learn how Strcmp In C++ enables low-level control and high-performance computing in the tutorial below.

The strcmp function in C++ compares null-terminated strings or two arrays of characters in lexicographical order. This function is a predefined library function in C++. It requires two character arrays as parameters for comparison. The strcmp function in C++ returns an integer value based on the position of the first differing character in the two strings.

What is STRCMP function in c++?

It is an inherent function provided by the C++ standard library. Within the header file of <cstring>, this function is declared and defined. This function operates by comparing strings passed as arguments to determine their equality. It examines each character at every index in both strings, facilitating a lexicographical comparison. The comparison process begins at the first character of each string and continues sequentially until a differing or null character is encountered.

Syntax of the strcmpfunction in c++:

When utilizing the strcmp function in C++, the syntax for this function is as follows:

Example

intstrcmp(const char *str1, const char *str2 );

in this format, the variables str1 and str2 represent the names of the strings respectively.

Parameter used in the strcmp function in c++:

The strcmp function compares the pointers to character arrays by using two parameters, namely str1 and str2.

Value returned by the strcmp function in c++:

An integer value is generated as output by the strcmp function in C++. This value is determined based on the position of the first differing character when comparing two strings in C++. The strcmp function in C++ produces three distinct types of return values.

1) Zero( 0 ):

Here, the function returns zero as the output. In this scenario, both strings are identical, and there exists a corresponding index in each of them.

Example:

Example

#include<iostream>
#include<cstring>
using namespace std;
int main() {
char str1[] = "data structure algorithm";
char str2[] = "data structure algorithm";
intreturnvalue = strcmp(str1, str2);
if (returnvalue == 0) {
cout<< "equal String";
} else {
cout<< "unequal String";
}
cout<< "\nstrcmp() return value is: " <<returnvalue;
return 0;
}

Output:

Output

equal String
strcmp() return value is: 0

2) greater than zero ( >0 ):

If the ASCII value of the first differing character on the left side of the string is higher than the corresponding character on the right side, the function will return a positive value. The result is the variance between the ASCII values of the first differing characters in the strings.

Example:

Example

#include<iostream>
#include<cstring>
using namespace std;
int main() {
char str1[] = "data structure algorithm";
char str2[] = "data structure ALGORIThm";
intreturnvalue = strcmp(str1, str2);
if (returnvalue == 0) {
cout<< "equal String";
} else {
cout<< "unequal String";
}
cout<< "\nstrcmp() return value is: " <<returnvalue;
return 0;
}

Output:

Output

unequal String
strcmp() return value is: 32

Explanation:

In the provided code, a pair of strings, str1 and str2, are defined and given initial values. These strings are subsequently compared using the strcmp function. The outcome of this comparison is saved in the variable named returnvalue. The initial disparity between the strings is identified at position 0, where the characters in both strings differ. Specifically, the ASCII values of these characters are 115 and 83. Consequently, the discrepancy in ASCII values is calculated as 32. A condition is employed to assess the equality of the strings by checking if returnvalue equals zero.

3) less than zero ( <0 ):

If the ASCII code of the character on the left side of the initial differing character in the string is smaller than the ASCII code of the corresponding character on the right side, the resulting value will be negative. The result value is determined by subtracting the ASCII values of the first differing characters in the strings, denoted as str1 - str2.

Example:

Example

#include<iostream>
#include<cstring>
using namespace std;
int main() {
char str1[] = "data structure ALGORIThm";
char str2[] = "data structure algorithm";
intreturnvalue = strcmp(str1, str2);
if (returnvalue == 0) {
cout<< "equal String";
} else {
cout<< "unequal String";
}
cout<< "\nstrcmp() return value is: " <<returnvalue;
return 0;
}

Output:

Output

unequal String
strcmp() return value is: -32

Prototype of the strcmp:

In C++, the prototype for strcmp is defined in the header file named cstring:

Example

intstrcmp(const char *str1, const char *str2);

The characters in strings such as str1 and str2 need to be compared lexicographically using the strcmp function.

The return value refers to the dissimilarity between the initial differing characters found in str1 and str2.

Undefined behavior of Strcmp:

In C++, when a parameter refers to null-terminated strings or C++ character arrays, using the function strcmp may result in exceptions or undefined behavior.

Example:

Example

#include<iostream>
#include<cstring>
using namespace std;
int main() {
string str1[] = "data structure";
string str2[] = "ALGORithm";
intreturnvalue = strcmp(str1, str2);
if (returnvalue == 0) {
cout<< "equal String";
} else {
cout<< "unequal String";
}
cout<< "\nstrcmp() return value is: " <<returnvalue;
return 0;
}

Compilation error:

Example

sss.cpp	In function 'int main()':
sss.cpp	[Error] cannot convert 'std::string* {aka std::basic_string<char>*}' to 'const char*' for argument '1' to 'intstrcmp(const char*, const char*)'

Output:

Output

no output

Using user-defined function strcmp function implementation:

Example

#include<iostream>

#include<cstring>
using namespace std;
voidcompareStr(char * str1, char * str2) {
intreturnvalue = strcmp(str1, str2);

if (returnvalue == 0)
cout<< "equal strings";
else
cout<< "unequal strings";

cout<< "\nstrcmp() return value is:  " <<returnvalue;
}
int main() {
char str1[] = "data structure algorithm";
char str2[] = "data structure algorithm";
compareStr(str1, str2);
return 0;
}

Output:

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 nocpp tutorial to a c character array or null terminating strings.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience