Pages

Saturday, 20 June 2015

User defined string functions to compare

User defined string functions to compare two given strings

Coding:

#include<stdio.h>
int mystrcmp(char *a,char *b)
{
int i;
for(i=0;(a[i]==b[i])&&(a[i]!='\0');i++);
return a[i]-b[i];
}

int main()
{
char a[30],b[30];
int k;
printf("Please enter the first string:");
gets(a);
printf("Please enter the second string:");
gets(b);
k=mystrcmp(a,b);
if(k>0)
printf("String 1 > string 2");
else if(k<0)
printf("String 1 < string 2");
else
printf("String 1 == string 2");
return 0;
}

Output:
Please enter the first string:ABC
Please enter the second string:ABc
String 1 < string 2