I am making a user defined function of strcmp() in c.
#include <stdio.h>
#include <string.h>
int la,lb; // la and lb for length of 2 strings
int scompare(char [],char []);
int main() {
char a[50],b[50]; // a and b are 2 strings
printf("Enter 2 strings \n");
gets(a);
gets(b);
la =strlen(a);
lb =strlen(b);
printf("%d",scompare(a,b));
}
int scompare(char a[la],char b[lb])
{
for(int i=0 ;i<max(la,lb);i++)
{ // loop for comparing characters of 2 strings
// here in vs code it is showing error --->
// warning: implicit declaration of function 'max' [-Wimplicit-function-declaration]
for(int i=0 ;i<max(la,lb);i++)
int k = a[i]-b[i];// k is the sum of ascii of characters of a and b
if(k!=0 &&toupper(a[i])==toupper(b[i]))
return (k>0)?1:-1;
// other error is showing here in function toupper ---> warning: implicit declaration of function 'toupper' [-Wimplicit-function-declaration]
if(k!=0 &&toupper(a[i])==toupper(b[i]))
else if(k!=0)
return k;
}
return 0;
}
Before calling a function you need to provide its declaration that can be present for example in a header.
There is no standard function max
in C. You need to write such a function yourself.
To use the standard function toupper
you need to include the header <ctype.h>
#include <ctype.h>
This loop (if to assume that the function max
is defined somewhere)
for(int i=0 ;i<max(la,lb);i++)
can invoke undefined behavior then lengths of strings are not equal to each other.
Pay attention to that the function gets
is not a standard C function. It is unsafe. You should use fgets
.
And this function declaration
int scompare(char a[la],char b[lb]);
does not make a sense.
The function should be declared like
int scompare( const char a[], const char b[] );