Compare Strings with strcmp Function in C

Compare Strings with strcmp Function in C

3 mins read468 Views Comment
Updated on Dec 21, 2023 14:37 IST

The strcmp function in C is used to compare two strings and determine whether they are equal or not. This article explains the syntax and usage of the strcmp function in detail, along with some examples to help you understand how it works.

2023_03_Strcmp-function-in-C.jpg

In this tutorial, we will discuss the strcmp function in C programming language that is used to compare C strings. As we will be covering the following methods, you may want to check up a little about C programming before reading ahead as a beginner.

So, without further ado, let’s get started!

Introduction to strcmp Function in C

In C programming language, strcmp() is a built-in string function that is used to compare two strings. The function returns an integer value that indicates the relationship between the strings being compared. 

The syntax for the strcmp function is:


 
int strcmp(const char *str1, const char *str2)
Copy code

Here, str1 and str2 are the two strings that are to be compared. The function returns an integer value as follows:

  • If str1 is equal to str2, then 0 is returned.
  • If str1 is greater than str2, then a positive integer is returned.
  • If str1 is less than str2, then a negative integer is returned.

The comparison is done character by character, starting from the first character of each string, until a mismatch is found or the end of the strings is reached. The ASCII value of each character is compared to determine their relationship.

All About Strlen() Function in C
All About Strlen() Function in C
Unveil the strlen() function in C programming, diving deep into its syntax, practical implementations, and code samples. Learn how to effectively utilise strlen() to determine string lengths, manipulate strings, and...read more
strcpy() Function in C
strcpy() Function in C
Uncover the versatility of the strcpy() function in C programming, exploring its syntax, practical applications, and code examples. Learn how strcpy() efficiently copies strings, manipulate substrings, and master string operations...read more
Understanding Strchr() Function in C
Understanding Strchr() Function in C
This blog explains the strchr() Function in C programming with the help of examples. Let’s understand!

C Examples of strcmp Function 

Example 1: Comparing two strings


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "hello";
char str2[20] = "world";
int result = strcmp(str1, str2);
if (result == 0) {
printf("The strings are equal\n");
}
else if (result > 0) {
printf("%s is greater than %s\n", str1, str2);
}
else {
printf("%s is less than %s\n", str1, str2);
}
return 0;
}
Copy code

Output

In this example, the strcmp function is used to compare two strings str1 and str2. Since the ASCII value of ‘h’ is less than the ASCII value of ‘w’, the result is less than 0. Hence, the output is hello is less than world.

Example 2: Comparing user input string with a predefined string


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Naukri";
char str2[20];
printf("Enter a string: ");
scanf("%s", str2);
int result = strcmp(str1, str2);
if (result == 0) {
printf("The strings are equal\n");
}
else {
printf("The strings are not equal\n");
}
return 0;
}
Copy code

Output 1

Output 2

In this example, we see that the strcmp function is case-sensitive.

Example 3: Comparing two strings of different lengths


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "hello";
char str2[20] = "world123";
int result = strcmp(str1, str2);
if (result == 0) {
printf("The strings are equal\n");
}
else if (result > 0) {
printf("%s is greater than %s\n", str1, str2);
}
else {
printf("%s is less than %s\n", str1, str2);
}
return 0;
}
Copy code

Output

In this example, the two strings str1 and str2 have different lengths. The comparison is done character by character until a mismatch is found or the end of the strings is reached. Since the ASCII value of ‘h’ is less than the ASCII value of ‘w’, the result is less than 0. Hence, the output is hello is less than world123.

Example 4: Checking if a string is empty


 
#include <stdio.h>
#include <string.h>
int main() {
char str[20] = "";
int result = strcmp(str, "");
if (result == 0) {
printf("The string is empty\n");
}
else {
printf("The string is not empty\n");
}
return 0;
}
Copy code

Output:

Strcomp function in C Empty output

In this example, the strcmp function is used to compare a string str with an empty string ” “. If both strings are equal, the string is empty.

Example 5: Checking if string is a palindrome 


 
#include <stdio.h>
#include <string.h>
int main() {
char str[20] = "racecar";
int i, j, len, flag = 1;
len = strlen(str);
for (i = 0, j = len - 1; i < len / 2; i++, j--) {
if (str[i] != str[j]) {
flag = 0;
break;
}
}
if (flag == 1) {
printf("%s is a palindrome\n", str);
}
else {
printf("%s is not a palindrome\n", str);
}
return 0;
}
Copy code

Output:

Output palindrome strcmp function in c

In this example, the strcmp function is not used directly, but it is used to check if a string str is a palindrome. First, the length of the string is determined using strlen(). Then, the characters of the string are compared from the start and end until the middle of the string is reached. If all the characters match, the string is a palindrome.

Example 6: Sorting an array of strings


 
#include <stdio.h>
#include <string.h>
int main() {
char strings[5][20] = {
"apple",
"banana",
"cherry",
"date",
"elderberry"
};
int i, j;
char temp[20];
for (i = 0; i < 5; i++) {
for (j = i + 1; j < 5; j++) {
if (strcmp(strings[i], strings[j]) > 0) {
strcpy(temp, strings[i]);
strcpy(strings[i], strings[j]);
strcpy(strings[j], temp);
}
}
}
printf("Sorted strings:\n");
for (i = 0; i < 5; i++) {
printf("%s\n", strings[i]);
}
return 0;
}
Copy code

Output:

Output array sorting

In this example, an array of strings strings is sorted in ascending order using the strcmp function. A nested loop is used to compare each string with every other string in the array. If the strcmp function returns a positive value, the strings are swapped using the strcpy() function [link here]. Finally, the sorted array of strings is printed to the console.

Endnotes

In conclusion, the strcmp function in C is a powerful tool for comparing strings. It’s important to remember that strcmp() compares strings character by character, and the result is based on the ASCII value of each character. This means that uppercase letters are considered to be less than lowercase letters, and punctuation marks and other special characters also have specific ASCII values. Learn more about such functions and apply taking up beginner-level C programming courses.

Contributed by Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio