How to compare two strings in c – C Program to Compare Two Strings

  • Write a C program to compare two strings.

Compare two strings in c: 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

How to compare two strings in c: 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.

C Program to Compare Two Strings

/*
* C Program to Compare two string using strcmp
*/
 
#include <stdio.h>
#include <conio.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);
   }
    
   getch();
   return 0;
}

Program 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

Write a program in c to compare two strings using pointers in a user defined function

Comparing two strings in c: 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.

Write a program in c to compare two strings using pointers in a user defined function

/*
* C Program to compare two string using function
*/
#include <stdio.h>
#include <conio.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);
   }
   getch();
   return 0;
}
 
/*
 * Function to compare two string
 * If return value < 0 then firstString is less than secondString
 * If return value > 0 then secondString is less than firstString
 * If return value = 0 then firstString is equal to secondString
 */
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];
}

Program 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