Check or Compare Two Strings Equal or not - C Programming Example

Algorithm


  1. We use string.h header library of C.
  2. Then do comparison using strcmp(string1,string2).
    1. If Difference = 0, then matched - strcmp(string1,string2) == 0
    2. Else Not matched

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <stdio.h>
#include <string.h>

int main()
{
   char string1[100], string2[100];

   printf("Enter string 1: \n");
   scanf("%s", string1);

   printf("Enter string 2: \n");
   scanf("%s", string2);

   if (strcmp(string1,string2) == 0) {
      printf("Strings are equal.\n");
   } else {
       printf("Strings are not equal.\n");
   }

   return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
Enter string 1: Akash
Enter string 2: Akash

Output

x
+
cmd
The strings are equal.

Demonstration


This is a very simple example of String comparison in C programming. We've used string.h header library of C programming.

Then just use the strcmp() function to comparison.

 

Run C Programming Code Online - https://devsenv.com/page/run-c-programming

 

 

For more example of string.h header library, check from here - https://www.cplusplus.com/reference/cstring