Advertisement
DrAungWinHtut

string_manipulation1.c

Sep 2nd, 2022
1,476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #pragma warning(disable : 4996)
  4. int strlen2(const char* arr);
  5. int modifyArray(char* arr);
  6. char* strcpy2(char* destination, const char* source);
  7. char* strcat2(char* destination, const char* source);
  8. int strcmp2(const char* str1, const char* str2);
  9. int main()
  10. {
  11.     char a[15] = { 'a','u','n','g',' '};
  12.     char b[] = { 'v','k','n','t','\0'};
  13.     char c[] = "aungb";
  14.     char d[] = "aunga";
  15.     char k[7] ;
  16.     char* array;
  17.     int result = strcmp(c, d);
  18.     printf("%d\n", result);
  19.    
  20.     //printf("strlen2(a)= %d  strlen2(b)= %d", strlen2(a), strlen2(b));
  21.    
  22. }
  23.  
  24. int strlen2(const char * arr)
  25. {
  26.     int count = 0;
  27.     while (arr[count] != '\0' && arr[count] != -52)
  28.     {
  29.         //printf("arr[%d]=%d=%c\n", count, arr[count], arr[count]);
  30.         count++;
  31.     }
  32.     printf("\n\n\n");
  33.     return count;
  34. }
  35.  
  36. int modifyArray(char* arr)
  37. {
  38.     arr[0] = 'p';
  39.     arr[1] = 'a';
  40.     arr[2] = 'd';
  41.     return 0;
  42. }
  43.  
  44. char* strcpy2(char* destination, const char* source)
  45. {
  46.     int count = 0;
  47.     while (source[count] != '\0' && source[count] != -52)
  48.     {
  49.         destination[count] = source[count];
  50.         count++;
  51.     }
  52.     destination[count] = '\0';
  53.     return destination;
  54. }
  55.  
  56. char* strcat2(char* destination, const char* source)
  57. {
  58.     int len1 = strlen2(source);
  59.     int len2 = strlen2(destination);
  60.     for (int i = 0; i < len1; i++)
  61.     {
  62.         destination[len2 + i] = source[i];
  63.     }
  64.     return destination;
  65. }
  66.  
  67. int strcmp2(const char* str1, const char* str2)
  68. {
  69.     int count = 0; 
  70.     int str1_len = strlen2(str1);
  71.     int str2_len = strlen2(str2);
  72.     if (str1_len > str2_len)
  73.     {
  74.         return 1;
  75.     }
  76.     if (str1_len < str2_len)
  77.     {
  78.         return -1;
  79.     }
  80.     if (str1_len == str2_len)
  81.     {
  82.         while (str2[count] != '\0' && str2[count] != -52)
  83.         {
  84.             if (str1[count] == str2[count])
  85.             {
  86.                 count++;
  87.             }
  88.             else
  89.             {
  90.                 return str1[count] - str2[count];
  91.             }
  92.         }
  93.     }  
  94.     return str1[count] - str2[count];
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement