C Program to check if two strings are same or not

Given two strings str1 and str2 we have to check whether the two strings are same or not. Like we are given two stings “hello” and “hello” so they are identical and same.

Identical are the strings which seems equal but are not equal like : “Hello” and “hello”, and same are the strings which are exactly same like : “World” and “World”.


Input: str1[] = {“Hello”}, str2[] = {“Hello”}
Output: Yes 2 strings are same
Input: str1[] = {“world”}, str2[] = {“World”}
Output: No, 2 strings are not same

The approach used below is as follows

We can use strcmp(string2, string1).

strcmp() string compare function is a in-built function of “string.h” header file, this function accepts two parameters, both strings. This function compares two strings and checks whether both strings are same and return 0 if there is no change in the string and return a non-zero value when the two strings are not same. This function is case-sensitive, means both the strings should be exactly same.

  • So we will take two strings as an input.
  • Use strcmp() and pass both the strings as parameters
  • If they return zero then print “Yes 2 strings are same”
  • Else print “No, 2 strings are not same”.


In function int main(int argc, char const *argv[])
   Step 1-> Declare and initialize 2 strings string1[] and string2[]
   Step 2-> If strcmp(string1, string2) == 0 then,
      Print "Yes 2 strings are same
"    Step 3-> else       Print "No, 2 strings are not same
" Stop


 Live Demo

#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[]) {
   char string1[] = {"tutorials point"};
   char string2[] = {"tutorials point"};
   //using function strcmp() to compare the two strings
   if (strcmp(string1, string2) == 0)
      printf("Yes 2 strings are same
");    else       printf("No, 2 strings are not same
" );       return 0; }


If run the above code it will generate the following output −

Yes 2 strings are same

Updated on: 21-Oct-2019

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started