ft_strcmp.c 771 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Reproduce the behavior of the function strcmp (man strcmp)
  2. #include <stdio.h>
  3. #include <string.h>
  4. int ft_strcmp(char *s1, char *s2)
  5. {
  6. int count, count2, i;
  7. count = 0;
  8. count2 = 0;
  9. i = 0;
  10. while (*s1++)
  11. count++;
  12. while (*s2++)
  13. count2++;
  14. if (count > count2)
  15. return (1);
  16. else if (count < count2)
  17. return (-1);
  18. while (s1[i] != '\0' && s1[i] == s2[i])
  19. i++;
  20. if (s1[i] > s2[i])
  21. return (1);
  22. else if (s1[i] < s2[i])
  23. return (-1);
  24. else
  25. return (0);
  26. }
  27. int main(void)
  28. {
  29. int a, b, c, d, e, f;
  30. a = ft_strcmp("Hello, World", "Abcd");
  31. b = ft_strcmp("Hi", "Hi");
  32. c = ft_strcmp("Aa", "Ab");
  33. d = strcmp("Hello, World", "Abcd");
  34. e = strcmp("Hi", "Hi");
  35. f = strcmp("Aa", "Ab");
  36. printf("%d, %d, %d, %d, %d, %d", a, b, c, d, e, f);
  37. return (0);
  38. }