Javatpoint Logo
Javatpoint Logo

Strcmp() function in C

The C standard library includes the strcmp() function for string comparison. In C programming, it is frequently used to compare two strings and is a component of the <string.h> header. A connection between the two strings is shown by the function's return value, which is an integer value.

The strcmp(first_string, second_string) function compares two strings and returns 0 if both strings are equal.

Pointer to the first string that will be compared, str1.

Pointer to the second string that will be compared, str2.

If str1 is lexicographically superior to str2, it returns an integer number larger than 0.

If str1 is lexicographically smaller to str2, it returns an integer value that is less than 0.

It returns 0 if str1 and str2 are equal.

Here, we are using gets()function, which reads string from the console.

Output

Enter 1st string: hello
Enter 2nd string: hello
Strings are equal

Explanation:

The program comes with the appropriate header files, including "stdio.h" for functions that deal with standard input/output and "string.h" for functions that deal with strings, including strcmp().

Two-character arrays str1 and str2 of size 20 are declared to hold the input strings supplied by the user.

Using the printf() method, the program asks the user to input the first string.

The input string is read from the console using the gets() method. However, it is significant to highlight that due to its susceptibility to buffer overflow attacks, gets() is deprecated and not advised. In a similar manner, the program asks the user to enter the second string before using gets() to read it.

After that, the two strings str1 and str2 are put to a comparison using the strcmp() method.

Since the two strings are equal if strcmp() returns 0, the program writes "Strings are equal" using printf().

Otherwise, the program outputs "Strings are not equal" if strcmp() returns a non-zero result.

Lastly, the main function returns 0, indicating that the program has successfully run.

Two strings are character-by-character compared using the strcmp() method. The comparison process begins with each string's first characters. If they are equal, it advances to the subsequent characters until either:

It decides which string is lexicographically bigger or smaller based on the differences between the matching characters, or it reaches the end of one or both strings.

The ASCII values of the characters are used in the comparison.

Uppercase and lowercase letters are viewed differently in the comparison since it takes the case into account.

The function presupposes that the strings handed in as input are null-terminated. A string that ends with the null character ('0') is known as a null-terminated string.

In this example, the gets() function is used to read strings from the console. It is important to note that the gets() method has been deprecated in the C programming language and is strongly discouraged. The main cause of the issue is that gets() has no method to control how many characters are read from the input, rendering it vulnerable to buffer overflow attacks that can result in security flaws.

It is advised to use fgets() in place of the console to safely read strings. To avoid buffer overflows, the fgets() function lets you set the maximum number of characters to read.


Next TopicC strrev()



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