C Program to Compare Two Strings

In this C program, we will compare two strings using strcmp function and by using a user defined function. We first take two string as input from user using gets function and store it in two character array. Now, we have to compare two strings lexicographically and return the result of comparison.

We can either use strcmp function of string.h header file to compare strings or write our own function to compare strings using pointers.
Comparison between two strings is case sensitive, means "TECHCRASHCOURSE" and "TechCrashCourse" are two different strings.


C program to compare two strings using strcmp function

To use strcmp function, we must include string.h header file in our program. Here is the declaration for strcmp() function.


int strcmp(const char *firstString, const char *secondString);
Return values of strcmp function
Return Value Description
Positive (>0) firstString is greater than secondString
Zero (==0) firstString is equal to secondString
Negative (<0) firstString is less than secondString

The strcmp() function compares two strings character by character from left to right. It returns the difference between the ascii value of first mismatched characters otherwise zero is both strings are equal.
In below program, it takes two strings as input from user using gets function and stores it in two character arrays 'firstString' and 'secondString'. Then we pass both strings to strcmp function and based upon it's response we print both strings are equal or not.

#include <stdio.h>
#include <string.h>
 
int main()
{
   char firstString[100], secondString[100];
 
   printf("Enter first string \n");
   gets(firstString);
 
   printf("Enter second string \n");
   gets(secondString);
   
   if(strcmp(firstString, secondString) == 0){
       printf("%s and %s are Equal\n",
         firstString, secondString);
   } else {
       printf("%s and %s are Not Equal\n",
         firstString, secondString);
   }
   
   return 0;
}
Output
Enter first string 
TechCrashCourse
Enter second string
Tech Crash Course
TechCrashCourse and Tech Crash Course are Not Equal
Enter first string 
Compare String
Enter second string
Compare String
Compare String and Compare String are Equal

C program to compare two strings using pointers

This program uses a user defined function compareString to compare two strings. It takes firstString and secondString character pointers as input parameters and does input validation(neither firstString nor secondString pointer should be NULL). Inside while loop, it compares characters of both strings at same index one by one till it finds a mismatch or null character in both. After comparing it returns the result as described above.

#include <stdio.h>

int compareString(char *firstString, char *secondString);
int main()
{
   char firstString[100], secondString[100];
 
   printf("Enter first string \n");
   gets(firstString);
 
   printf("Enter second string\n");
   gets(secondString);
   
   if(compareString(firstString, secondString) == 0){
       printf("%s and %s are Equal\n",
          firstString, secondString);
   } else {
       printf("%s and %s are Not Equal\n",
          firstString, secondString);
   }
   return 0;
}


int compareString(char *firstString, char *secondString){
    if(firstString == NULL && secondString == NULL)
        return 0;
    int counter = 0;
    while(firstString[counter] == secondString[counter]){
        if(firstString[counter]=='\0' && secondString[counter]=='\0')
            return 0;
        counter++;
    }
    return firstString[counter] - secondString[counter];
}
Output
Enter first string 
Strcmp
Enter second string
strcmp
Strcmp and strcmp are Not Equal
Enter first string 
Compare String
Enter second string
Compare String
Compare String and Compare String are Equal

Related Topics
C program to concatenate strings
C program to find length of string
C program to check string is palindrome
C program to find a substring from a given string
C program to remove vowels from a string
C program to remove extra spaces from string
C Program to find frequency of characters in a string
List of all C programs