The library of C programming language is extremely rich in functions. We have already looked at Strcpy and Strncpy in C. This time we are going to look at Strcmp in C which stands for string compare. The String Compare function, as the name suggests, is used to compare the given strings. To understand the working of this function, let us take an example:
There are two strings s1 and s2. s1 points to an object while s2 points to another. The ‘strcmp’ function will return a negative integer value in case the object pointed to by s1 is less than the object pointed to by s2. The function will return 0 if the objects are equal, and a positive integer value will be returned if the s1 object is greater than the s2 object.
Let us narrow it down to a table for easy understanding.
In the below table, o1 is the object pointed to by s1, and o2 is the object pointed to by s2.
|Value Returned By Function||Condition|
Syntax of Strcmp Function in C
int strcmp (const char*string1, const char*string2);
Here, two strings are pointing to two varied objects that are being compared by the strcmp functions.
Point to Remember: While writing programs that use strcmp function, remember to use the header file <string.h>
Program to Compare Two Strings Using Strcmp Function in C
What are preprocessor directives in C?
- Include header files “stdio.h” and “string.h”.
- Start the main function. The main function contains arguments – int argc and const char*argv
- Declare a variable ‘ans’, as an integer.
- Initialize string o1 and o2.
- Use strcpy function to copy strings to o1 and o2. Copy “
- ans = strcmp (o1, o2).
- Start an if condition where if ans is equals to 0, it will print “Both strings are equal”. If ans is less than 0, then it will print “o2 is less than o1”; Else, “o2 is greater than o1” will print on the screen.
int main(int argc, const char*argv)
int ans; //Declare Variable ‘ans’
char o1=”This is Strcmp Function”;
char o2=”This is Strcmp Function in C”;
//Strcmp Function Compare Two Strings in C
ans = strcmp(o1, o2);
printf (“Both strings are equal\n”);
printf (“o2 is less than o1\n”);
printf(“o2 is greater than o1\n”);
How Dangling Pointer Affects Your Programming?
o2 is less than o1
The program is started by including the necessary header files <stdio.h> and <string.h>. While starting the main function, it is not necessary to put the arguments. Now inside the main function, we need a memory where the result can be stored so we create an integer type variable called ans.
Learn Recursion in C Programming
Two characters of length 50 and name o1 and o2 are initialized.
We use the strcmp function against ans containing arguments o1 and o2 (because we need to compare these two strings). After that, we write a few conditions.
If ans is 0, then both strings are equal. The value of ‘ans’ is less than 0, then o2 is less than o1. If ans is greater than 0, then o2 is greater than 01.
C Program to Compare Two Strings Without Using strcmp() Function
- Initialize String s1 and s2.
- Calling Str_cmp function
- Define function str_cmp with arguments s1 and s2 of type char.
- Declare a variable called n of type integer and initialize it to 0.
- Start a while loop in which is1 and s2 are equal. Inside the loop, run an if condition in which if s1 and s2 are empty, the condition will break. If s1 and s2 are equal, return 0, else return -1.
Program for Strcmp Implementation in C
int str_cmp (char , char ); //Declare Str_cmp function
char s1=”Write a String”; //Initialize string s1.
char s2=”Input a string”; //Initialize string s2.
if (str_cmp(s1,s2)==0) //Calling str_cmp function
printf(“Strings is equal.\n”);
printf(“Strings not equal.\n”);
//Definition of str_cmp function
int str_cmp (char s1, char s2)
if (s1[n]==’\0′ || s2[n]==’\0′)
if (s1[n]==’\0′ && s2[n]==’\0′)
Strings not Equal
We create a function called str_cmp which will help us in creating the strings. The function has a while loop with the initial condition that the characters in both the strings are equal. Inside the while loop, if both the strings are empty, the condition breaks. If strings are equal, 0 is returned, else -1 is returned.
Inside the main function, two strings s1 and s2 are taken from the user. The str_cmp function that we created to print “Strings is equal” on the screen. Otherwise, it will print “Strings not equal” on the screen.