C program to compare two strings using string function(strcmp)
This C program is to compare two strings using string function(strcmp).String function strcmp compares the two strings.
Do not forget to include ‘string.h’ header file.
Return values strcmp(str1,str2)
- if Return value < 0 then it indicates str1 is less than str2.
- if Return value > 0 then it indicates str2 is less than str1.
- if Return value = 0 then it indicates str1 is equal to str2.
Logic
We just compare the two strings.This C library function strcmp(str1,str2) will compare string str1 with string str2.
Any other further queries, you can leave it in the comment box or send me a mail.
Program
#include<stdio.h> #include<string.h> void main() { char str1[100],str2[100]; printf("Enter string str1\n"); gets(str1); printf("Enter string str2\n"); gets(str2); if(strcmp(str1,str2)==0) { printf("The two string are EQUAL!!!\n"); printf("%s(str1) = %s(str2)",str1,str2); } else { printf("The two string are NOT EQUAL!!!\n"); printf("%s(str1) != %s(str2)",str1,str2); } }