How to compare the two strings in C

Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with the help of string function, i.e., strcmp(), which is defined in a string.h header file.

String comparison by using string function

The string function which is pre-defined in a string.h header file is a strcmp() function. The strcmp() function consider two strings as a parameter, and this function returns an integer value where the integer value can be zeropositive or negative.

The syntax of the strcmp() function is given below:

int strcmp (const char* str1, const char* str2);  

In the above syntax, two parameters are passed as strings, i.e., str1 and str2, and the return type is int means that the strcmp() returns an integer value.

The strcmp() function compares the character of both the strings. If the first character of both the strings are same, then this process of comparison will continue until all the characters are compared or the pointer points to the null character '\0'.

Possible return values from the strcmp() function

 

Return value Description
0 When both the strings are equal.
<0 If the ASCII value of a character of the first string is less than the ASCII value of a character of the second string, then the function will return negative value.
>0 If the ASCII value of a character of the first string is greater than the ASCII value of a character of the second string, then the function will return positive value.
 

Let's understand through an example.

#include   
#include  
int main()  
{  
   char str1[20];  // declaration of char array  
   char str2[20];  // declaration of char array  
   int value; // declaration of integer variable  
   printf("Enter the first string : ");  
   scanf("%s",str1);  
   printf("Enter the second string : ");  
   scanf("%s",str2);  
   // comparing both the strings using strcmp() function  
   value=strcmp(str1,str2);  
   if(value==0)  
   printf("strings are same");  
   else  
   printf("strings are not same");  
   return 0;  
}  

Analysis of the above program

  • We have declared two arrays of char type, i.e., str1 and str2. We take the user input as strings.
  • We compare the strings by using the strcmp() function, i.e., strcmp(str1,str2). This function will compare both the strings str1 and str2. If the function returns 0 value means that both the strings are same, otherwise the strings are not equal.

String comparison without using strcmp() function

#include   
int compare(char[],char[]);  
int main()  
{  
   char str1[20]; // declaration of char array  
   char str2[20]; // declaration of char array  
   printf("Enter the first string : ");  
   scanf("%s",str1);  
   printf("Enter the second string : ");  
   scanf("%s",str2);  
   int c= compare(str1,str2); // calling compare() function  
   if(c==0)  
   printf("strings are same");  
   else  
   printf("strings are not same");  
  
    return 0;  
}  
  
// Comparing both the strings.  
int compare(char a[],char b[])  
{  
    int flag=0,i=0;  // integer variables declaration  
    while(a[i]!='\0' &&b[i]!='\0')  // while loop  
    {  
       if(a[i]!=b[i])  
       {  
           flag=1;  
           break;  
       }  
       i++;  
    }  
    if(flag==0)  
    return 0;  
    else  
    return 1;  
}  

Analysis of the above program

  • In the above, we have declared two arrays of char type, and we take the user input as strings.
  • We have defined a compare() function which will take user input strings as a parameter, and compare both the strings. If the function returns 0, which means that both the strings are equal otherwise both the strings are not equal.

 

String comparison by using pointers

#include   
int stringcompare(char*,char*);  
int main()  
{  
  char str1[20]; // declaration of char array  
  char str2[20]; // declaration of char array  
  printf("Enter the first string : ");  
  scanf("%s",str1);  
  printf("\nEnter the second string : ");  
  scanf("%s",str2);  
  int compare=stringcompare(str1,str2); // calling stringcompare() function.  
  if(compare==0)  
  printf("strings are equal");  
 else  
 printf("strings are not equal");  
return 0;  
}  
// Comparing both the strings using pointers  
int stringcompare(char *a,char *b)  
{  
   int flag=0;  
    while(*a!='\0' && *b!='\0')  // while loop  
    {  
        if(*a!=*b)  
        {  
            flag=1;  
        }  
        a++;  
        b++;  
    }  
      
    if(flag==0)  
    return 0;  
    else  
    return 1;  
}  

Analysis of the above program

  • We have created two arrays of char type str1 and str2. We take the user input as strings.
  • We have defined a stringcompare() function which will take two pointers of char type as a parameter. The 'a' pointer holds the address of str1 and 'b' pointer holds the address of str2. Inside the function, we have created a while loop which will execute until the pointer a or b is not reached to a null character.

 

Submit Your Programming Assignment Details