Compare 2 Strings
String is an array of characters whose last element is '0'. Two compare two strings they should be equal in length and every single character should match.
/*
http://www.ProbCOMP.com
String is an array of characters whose last element is '\0'. Two compare two strings they should be equal in length and
every single character should match.
*/
#include<stdio.h>
#include<string.h>
int stringcompare(char *string1, char *string2)
{
if(strlen(string1) != strlen(string2))
return 0;
else
for(int i=0;string1[i]!='\0';i++)
if(string1[i]!=string2[i])
return 0;
return 1;
}
void main()
{
char str1[]="hello";
char str2[]="world";
int flag = stringcompare(str1,str2);
if(flag)
printf("%s and %s are equal",str1,str2);
else
printf("%s and %s are not equal",str1,str2);
}
Download Program


