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 How to Compare Two Strings Without Using Strcmp in C. 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 mentioned below:
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 |
negative | o1<o2 |
zero | o1=o2 |
positive | o1>o2 |
Table of Contents
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?
Algorithm
- Start.
- 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.
- End
Program
#include <stdio.h>
#include <string.h>
int main(int argc, const char*argv[])
{
int ans; //Declare Variable ‘ans’
char o1[50]=”This is Strcmp Function”;
char o2[50]=”This is Strcmp Function in C”;
//Strcmp Function Compare Two Strings in C
ans = strcmp(o1, o2);
if (ans==0)
printf (“Both strings are equal\n”);
if(ans<0)
printf (“o2 is less than o1\n”);
else
printf(“o2 is greater than o1\n”);
return 0;
}
How Dangling Pointer Affects Your Programming?
Output
o2 is less than o1
Explanation
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
Algorithm
- Start
- 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.
- End
Program for Strcmp Implementation in C
#include <stdio.h>
int str_cmp (char [], char []); //Declare Str_cmp function
int main()
{
char s1[50]=”Write a String”; //Initialize string s1.
char s2[50]=”Input a string”; //Initialize string s2.
if (str_cmp(s1,s2)==0) //Calling str_cmp function
printf(“Strings is equal.\n”);
else
printf(“Strings not equal.\n”);
return 0;
}
//Definition of str_cmp function
int str_cmp (char s1[], char s2[])
{
int n=0;
while(s1[n]==s2[n])
{
if (s1[n]==’\0′ || s2[n]==’\0′)
break;
n++;
}
if (s1[n]==’\0′ && s2[n]==’\0′)
return 0;
else
return -1;
}
Output
Strings not Equal
Explanation
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.
Also read: Operators in C
What are Storage Classes in C?