0

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;
 } 
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • "int k = a[i]-b[i]; k is the sum of ascii of characters of a and b" - are you sure?:) – Vlad from Moscow Jul 05 '21 at 09:09
  • 1
    Also, [you shouldn't be using the `gets` function](https://stackoverflow.com/q/1694036/10871073). – Adrian Mole Jul 05 '21 at 09:16
  • The code when `k` is non-zero but the characters are the same except for case is dubious; the return of `(k>0)?1:-1` is not much different from `return k`—if it is being used merely to indicate ordering. Is `scompare` supposed to do a case-insensitive comparison? What is it supposed to do? What is the purpose of `if(k!=0 &&toupper(a[i])==toupper(b[i])) return (k>0)?1:-1;`? – Eric Postpischil Jul 05 '21 at 10:25

3 Answers3

1

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[] );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Re “And this function declaration `int scompare(char a[la],char b[lb]);` does not make a sense”: That declares `scompare` to be a function that returns an `int` and takes parameters `a` and `b` that are arrays of `char` with `la` and `lb` elements, respectively. `la` and `lb` were declared earlier. – Eric Postpischil Jul 05 '21 at 09:58
  • @EricPostpischil And what? What is the sense of using la and lb in the function declaration? – Vlad from Moscow Jul 05 '21 at 10:05
  • It conveys to the reader how long the arrays are expected to be. – Eric Postpischil Jul 05 '21 at 10:11
  • @EricPostpischil You are wrong. This has nothing common with lengths of arrays. It only makes the function declaration dependent on global variables. – Vlad from Moscow Jul 05 '21 at 10:13
  • It is routine to declare array parameters in functions with all their dimensions even if the first dimension is ignored by the compiler, as a way of documenting the expected array length. The function is in fact dependent on global variables regardless of whether they are used in the parameters or not, because they are used in the code inside the function. That is bad design, but that is the way it is, and the meaning of the parameter declarations as shown in the question is clear. – Eric Postpischil Jul 05 '21 at 10:15
  • @EricPostpischil The user of the function sees its declaration before main without la and lb. These statements la =strlen(a); lb =strlen(b); should be placed inside the function instead of declaring parameters with la and lb. – Vlad from Moscow Jul 05 '21 at 10:18
  • Yes, the function should be defined differently. That is unrelated to the fact that declaring array parameters with dimensions is routine (ordinary), conforming C, well-defined, and clear. – Eric Postpischil Jul 05 '21 at 10:19
  • Given that you are critiquing OP’s code beyond what was asked in the question, you missed an actual problem that could cause the program to misbehave: `toupper(a[i])` should be `toupper((unsigned char) a[i])`, and similarly for `toupper(b[i])`. – Eric Postpischil Jul 05 '21 at 10:20
  • @EricPostpischil It is unclear because reading the function definition the user does not know how la and lb are obtained that is whether they are sizes of arrays or lengths of strings. – Vlad from Moscow Jul 05 '21 at 10:22
  • Judging from the code, they are lengths of arrays, as the code uses `i – Eric Postpischil Jul 05 '21 at 10:24
0

In your code, max and toupper are not defined in any headers. I think it must be like bellow.

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define max(a,b)            (((a) > (b)) ? (a) : (b))

int la, lb;

int scompare(char [], char []);

int main()
{
    char a[50], b[50];

    printf("Enter 2 strings \n");
    gets(a);
    gets(b);

    la = strlen(a);
    lb = strlen(b);
    printf("%d\n", scompare(a, b));
}

int scompare(char a[], char b[])
{
    for (int i = 0; i < max(la, lb); i++)
    {
        int k = a[i] - b[i];
        if (k && toupper(a[i]) == toupper(b[i]))
        {
            return (k > 0 ? 1 : -1);
        }
        else if (k)
        {
            return k;
        }
    }
    return 0;
}

Now it will be built successfully, and result is enter image description here

But, I think that your scompare function is not efficient. In my opinion

int scompare(char a[], char b[])
{
    for (int i = 0; i < max(la, lb); i++)
    {
        int k = a[i] - b[i];
        if (k == 0) continue;

        return k > 0 ? 1 : -1;
    }
    return 0;
}
secuman
  • 539
  • 4
  • 12
0

If you use functions from other libraries, you need to include these libraries:

#include <ctype.h>

...

if(k!=0 &&toupper(a[i])==toupper(b[i]))
         return (k>0)?1:-1;
...

If you use your own function, you need to declare it before usage and define it in the same file. Lets assume that int max(int, int) is your function:

#include <stdio.h>

/* function declaration */
int max(int lhs, int rhs);

...

/* usage */
int scompare(char a[la],char b[lb])
   {
     for(int i=0 ;i<max(la,lb);i++)
     {

...

/* definition */
int max(int lhs, int rhs)
{
    /* implementation */
}
Igor_M
  • 308
  • 2
  • 12