C program to compare the strings.

 

01 #include<stdio.h>
02
03 int stringCompare(char[],char[]);
04
05 int main()
06 {
07 char str1[100],str2[100];
08 int compare;
09
10 printf("Enter first string: ");
11 gets(str1);
12 printf("Enter second string: ");
13 gets(str2);
14
15 compare = stringCompare(str1,str2);
16
17 if(compare == 1)
18 printf("Both strings are equal.");
19 else
20 printf("Both strings are not equal");
21
22 return 0;
23 }
24
25 int stringCompare(char str1[],char str2[])
26 {
27 int i=0,flag=0;
28
29 while(str1[i]!='' && str2[i]!='')
30 {
31 if(str1[i]!=str2[i])
32 {
33 flag=1;
34 break;
35 }
36
37 i++;
38 }
39
40 if(flag==0 && str1[i]=='' && str2[i]=='')
41 return 1;
42 else
43 return 0;
44 }
 
 OUTPUT :

(1st Run)

Enter first string: Hello

Enter second string: Hello

Both strings are equal.


(2nd Run)

Enter first string: Hello

Enter second string: Hell

Both strings are not equal.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *