C language string size comparison

  • 2020-04-02 03:12:19
  • OfStack

C language string size comparison


#include <stdio.h>
#include <string.h>
 
int fun(char *a,char *b)
{
  int i = 0,j = 0;
  while(a[i]&&b[j])
  {
    if(a[i]-b[j]>0)
      return 1;
    else if(a[i]-b[j]<0)
      return -1;
    i++,j++;
     
  }
  if(strlen(a)==i&&strlen(b)==j)
    return 0;
  else if(strlen(a)==i)
  {
    return -1;
  }
  else
    return 1;
}
 
int main()
{
  char a[100];
  char b[100];
  printf("n");
  printf("******* Compare the size of two strings :*******n");
  printf("n");
  printf(" Enter the first string: ");
  scanf("%s",a);
  printf(" Enter the second string: ");
  scanf("%s",b);
  printf("%dn",fun(a,b));
  return 0;
}

The above is all the content of this article, I hope you can enjoy it.


Related articles: