You are on page 1of 3

strcmp()

Syntax:
#include <string.h>
int strcmp( const char *str1, const char *str2 );

Description:
The function strcmp() compares str1 and str2, then returns:
Return value

Explanation

------------

-----------

less than 0

str1 is less than str2

equal to 0

str1 is equal to str2

greater than 0 str1 is greater than str2

Example:
printf( "Enter your name: " );
scanf( "%s", name );
if( strcmp( name, "Mary" ) == 0 )
printf( "Hello, Dr. Mary!\n" );

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

/* Sample C program for the strcmp function*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char nm[20],nm2[20];
clrscr();
puts("Enter a string - ");
gets(nm);
puts("Enter another string to know if both the strings are equal");
gets(nm2);
if(strcmp(nm,nm2)==0)
{
printf("Both the strings are equal");
}
else
{
printf("Strings are not equal");
}
getch();
}

C program to compare two strings using strcmp


#include<stdio.h>
#include<string.h>
main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}

You might also like