Write a function that accepts 2 strings and compares them.

#include<stdio.h>
#include<conio.h>

int strcmp(char [],char []);
void main()
{
int i;
char a[80],b[80];
clrscr();
printf("Enter 1st string\n");
gets(a);
printf("Enter string to compare\n");
fflush(stdin);
gets(b);
i=strcmp(a,b);
printf("%d",i);
if(i==0)
printf("Strings are same\n");
else
printf("\nNot same");

getch();
}
 int strcmp(char x[],char y[])
 {
 int j;
 for(j=0;x[j]&&y[j];j++)
 {
 if(x[j]!=y[j])
 return(x[j]-y[j]);
 }
 return(0);
}

1 comment:

Related Post

Array:
String functions:
General (Loops-for/while)
Write a Program to print reverse of a given number Write a Program to print Product of Digits (eg. 212=2*1*2=4) of a given number Write a C++ Program to Convert Binary into Decimal Number