Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. char *strstr(const char *haystack, const char *needle)
  2. {
  3. char *h;
  4. size_t needle_len;
  5. size_t i;
  6. size_t j;
  7.  
  8. h = (char *)haystack;
  9. if (!(needle_len = strlen(needle)))
  10. return (h);
  11. i = 0;
  12. while (h[i])
  13. {
  14. j = 0;
  15. while (needle[j] && needle[j] == h[i + j])
  16. j++;
  17. if (j == needle_len)
  18. return (&h[i]);
  19. i++;
  20. }
  21. return (NULL);
  22. }
  23.  
  24. int strncmp(const char *s1, const char *s2, size_t n)
  25. {
  26. if (!n)
  27. return (0);
  28. while (*s1 == *s2 && *s1 && --n)
  29. {
  30. s1++;
  31. s2++;
  32. }
  33. return ((int)((unsigned char)*s1 - (unsigned char)*s2));
  34. }
  35.  
  36. char *strncat(char *restrict s1, const char *restrict s2, size_t n)
  37. {
  38. size_t i;
  39. size_t len;
  40.  
  41. i = 0;
  42. len = strlen(s1);
  43. while (s2[i] && i < n)
  44. {
  45. s1[len + i] = s2[i];
  46. i++;
  47. }
  48. s1[len + i] = '\0';
  49. return (s1);
  50. }
  51.  
  52. char *strchr(const char *s, int c)
  53. {
  54. while (*s)
  55. {
  56. if (*s == (char)c)
  57. return ((char *)s);
  58. s++;
  59. }
  60. if (!*s && c == '\0')
  61. return ((char *)s);
  62. return (NULL);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement