Advertisement
avr39ripe

cppStrManipulation

May 19th, 2021
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. char* strDelIdx(char* str, int idx)
  4. {
  5.     char* start{ str };
  6.     str += idx;
  7.     while (*str)
  8.     {
  9.         *str = *(str + 1);
  10.         ++str;
  11.     }
  12.     return start;
  13. }
  14.  
  15. char* strDelCh(char* str, const char symbol)
  16. {
  17.     char* start{ str };
  18.     char* ins{ str };
  19.     while (*ins != symbol and *ins++);
  20.  
  21.     if (!*ins) { return start; };
  22.  
  23.     str = ins;
  24.     while (*str++)
  25.     {
  26.  
  27.         if (*str != symbol)
  28.         {
  29.             *ins++ = *str;
  30.         }
  31.     }
  32.  
  33.     return start;
  34. }
  35.  
  36. char* strInsCh(char* str, const char symbol, int idx)
  37. {
  38.     char* start{ str };
  39.     int len{ 0 };
  40.     --idx;
  41.     while (*str++) ++len;
  42.     while (len != idx)
  43.     {
  44.         *str = *(str - 1);
  45.         --str;
  46.         --len;
  47.     }
  48.     *str = symbol;
  49.     return start;
  50. }
  51.  
  52. char* strInsChAlt(char* str, const char symbol, int idx)
  53. {
  54.     char* start{ str };
  55.     str += idx;
  56.     char copyFirst{ *str };
  57.     *str = symbol;
  58.     char copySecond;
  59.     while (*str++)
  60.     {
  61.         copySecond = *str;
  62.         *str = copyFirst;
  63.         copyFirst = copySecond;
  64.     }
  65.  
  66.     return start;
  67. }
  68.  
  69. int main()
  70. {
  71.     char str[50]{ "Hello, bright world!" };
  72.     std::cout << str << '\n';
  73.     std::cout << strDelIdx(str, 5) << '\n';
  74.     std::cout << strDelCh(str, 'l') << '\n';
  75.     //std::cout << strInsChAlt(str, '#', 15) << '\n';
  76.     std::cout << strInsChAlt(str,'l',14) << '\n';
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement