Compare Strings with strcmp Function in C
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.
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)
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.
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;}
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;}
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;}
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;}
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;}
Output:
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;}
Output:
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
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