Advertisement
avr39ripe

cppStrManip

May 19th, 2021
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int strLen(const char* str)
  4. {
  5.     int len{ 0 };
  6.  
  7.     //while (true)
  8.     //{
  9.     //  if (*str == '\0')
  10.     //  {
  11.     //      break;
  12.     //  }
  13.     //  ++str;
  14.     //  ++len;
  15.     //}
  16.  
  17.     //while ((*(str++)))
  18.     //{
  19.     //  ++len;
  20.     //}
  21.    
  22.     for (; *str++; ++len);
  23.  
  24.     return len;
  25. }
  26.  
  27. char* strCpy(const char* src, char* dest)
  28. {
  29.     char* start{ dest };
  30.  
  31.     while (*dest++ = *src++);
  32.  
  33.     return start;
  34. }
  35.  
  36. char* strNCpy(const char* src, int destSize, char* dest)
  37. {
  38.     char* start{ dest };
  39.  
  40.     for (--destSize; *dest++ = (destSize > 0 ? *src++ : '\0'); --destSize);
  41.  
  42.     //for (int cnt{ 0 }; *(dest + cnt) = (cnt < destSize ? *(src + cnt) : '\0'); ++cnt);
  43.  
  44.     return start;
  45. }
  46.  
  47.  
  48. char* strCat(const char* src, char* dest)
  49. {
  50.     char* start{ dest };
  51.  
  52.     //dest += strLen(dest);
  53.     //strCpy(src, dest);
  54.  
  55.     while (*dest++);
  56.     --dest;
  57.     while (*dest++ = *src++);
  58.  
  59.     return start;
  60. }
  61.  
  62. char* strNCat(const char* src, int destSize, char* dest)
  63. {
  64.     char* start{ dest };
  65.  
  66.     //dest += strLen(dest);
  67.     //strCpy(src, dest);
  68.  
  69.     while (*dest++);
  70.     --dest;
  71.     for (--destSize; *dest++ = (destSize > 0 ? *src++ : '\0'); --destSize);
  72.  
  73.     return start;
  74. }
  75.  
  76. int main()
  77. {
  78.     char str[70]{ "are you? ;)" }; // char* const str
  79.     char* str1{ new char[70]{"Hello, how"} };
  80.     char* str3{ new char[] {"\nI'm fine!"} };
  81.        
  82.     std::cout << strCat(str3, strCat(str, str1)) << '\n'; //str1 -> "Hello, world!"
  83.  
  84.     delete[] str1;
  85.     delete[] str3;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement