Advertisement
LLubomirov

reverse string

Jul 5th, 2022
735
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. void print_backwards(const char* begin, const char* end)
  5. {
  6.     int pos = end - begin - 1;
  7.     int offset = 0;
  8.  
  9.     if(begin == end || pos == 0)
  10.     {
  11.         std::cout << begin[0];
  12.         return;
  13.     }
  14.  
  15.     while((int)*(begin + pos) <= 32 && pos >= 0)
  16.     {
  17.         --pos;
  18.     }
  19.  
  20.     while((int)*(begin + pos) > 32 && pos > 0)
  21.     {
  22.         ++offset;
  23.         --pos;
  24.     }
  25.  
  26.     for(size_t i = 0; i < offset; ++i)
  27.     {
  28.         std::cout << begin[pos + 1 + i];
  29.     }
  30.     std::cout << " ";
  31.    
  32.     print_backwards(begin, begin + pos);    
  33. }
  34.  
  35. void print_backwards(const char* string)
  36. {
  37.     std::cout << "'";
  38.     print_backwards(string, string + strlen(string));
  39.     std::cout << "'";
  40. }
  41.  
  42. // Извикването print_backwards("I\tneed a break!"); извежда низа "break! a need I".
  43. int main()
  44. {
  45.     const char* str = "I\tneed a break!";
  46.     print_backwards(str);
  47.     return 0;
  48. }
Advertisement
RAW Paste Data Copied
Advertisement