Advertisement
DrAungWinHtut

ghlib.c

Dec 20th, 2022
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1.  
  2. int strlen_k(const char *str)
  3. {
  4.     int count = 0;
  5.     while (str[count] != '\0' && str[count] != -52)
  6.     {
  7.         count++;
  8.     }
  9.     return count;
  10. }
  11.  
  12. void strcpy_k(char *destination, const char *source)
  13. {
  14.     int count = 0;
  15.     while (source[count] != '\0' && source[count] != -52)
  16.     {
  17.         destination[count] = source[count];
  18.         count++;
  19.     }
  20. }
  21.  
  22. int strcmp_k(const char *str1, const char *str2)
  23. {
  24.  
  25.     int index = 0;
  26.     int max = 0;
  27.     if (strlen_k(str1) > strlen_k(str2))
  28.     {
  29.         return 1;
  30.     }
  31.     else if (strlen_k(str1) < strlen_k(str2))
  32.     {
  33.         return -1;
  34.     }
  35.     while (str1[index] != '\0' && str1[index] != -52)
  36.     {
  37.         if (str1[index] > str2[index])
  38.         {
  39.             return 1; // str1 > str2
  40.         }
  41.         else if (str1[index] < str2[index])
  42.         {
  43.             return -1; // str1 < str2
  44.         }
  45.         index++;
  46.     }
  47.     return 0; // equal
  48. }
  49.  
  50. // homework
  51. // 1 - strncpy
  52. // 2 - strncmp
  53.  
  54. void strcat_k(char *destination, const char *source)
  55. {
  56.     int dl = strlen_k(destination);
  57.     int sl = strlen_k(source);
  58.     for (int i = 0; i < sl; i++)
  59.     {
  60.         destination[dl + i] = source[i];
  61.     }
  62. }
  63.  
Tags: c lib
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement