Search Your Topic

config
Thursday, 22 June 2017 03:14

C Compare String: strcmp()

Written by
Rate this item
(0 votes)

 

The strcmp(first_string, second_string) function compares two string and returns 0 if both strings are equal.

Here, we are using gets() function which reads string from the console.

 
  1. #include <stdio.h>  
  2. void main()  
  3. {  
  4.    char str1[20],str2[20];  
  5.   printf("Enter 1st string: ");  
  6.   gets(str1);//reads string from console  
  7.   printf("Enter 2nd string: ");  
  8.   gets(str2);  
  9.   if(strcmp(str1,str2)==0)  
  10.       printf("Strings are equal");  
  11.   else  
  12.       printf("Strings are not equal");  
  13. }  

Output:

Enter 1st string: hello
Enter 2nd string: hello
Strings are equal
Read 112940 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.