Advertisement
Filipbg

StringConcatenator

Mar 15th, 2020
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <string>
  2.  
  3. #include "StringConcatenator.h"
  4. using namespace std;
  5.  
  6. StringConcatenator::StringConcatenator() = default;
  7. StringConcatenator::~StringConcatenator() = default;
  8.  
  9. string StringConcatenator::concatenate(const ConcatenateStrategy strategy,
  10.                                        const char*               left,
  11.                                        const size_t              leftSize,
  12.                                        const char*               right,
  13.                                        const size_t              rightSize) const
  14.     {
  15.     string output = "";
  16.  
  17.     if(strategy == ConcatenateStrategy::LEFT_1_RIGHT_1)
  18.     {
  19.         string currentLeft = left;
  20.         string currentRight = right;
  21.  
  22.         for(size_t i = 0; i < leftSize + rightSize; ++i)
  23.         {
  24.             if(currentLeft.empty())
  25.             {
  26.                 output.append(currentRight);
  27.                 break;
  28.             }
  29.             if(currentRight.empty())
  30.             {
  31.                 output.append(currentLeft);
  32.                 break;
  33.             }
  34.             output.append(sizeof(char), left[i]);
  35.             currentLeft.erase(currentLeft.begin() + 0);
  36.             output.append(sizeof(char), right[i]);
  37.             currentRight.erase(currentRight.begin() + 0);
  38.         }
  39.         return output;
  40.     }
  41.     else if(strategy == ConcatenateStrategy::LEFT_2_RIGHT_1)
  42.     {
  43.         string currentLeft = left;
  44.         string currentRight = right;
  45.  
  46.         for(size_t i = 0; i < leftSize + rightSize;)
  47.         {
  48.             if(currentLeft.empty())
  49.             {
  50.                 output.append(currentRight);
  51.                 break;
  52.             }
  53.             if(currentRight.empty())
  54.             {
  55.                 output.append(currentLeft);
  56.                 break;
  57.             }
  58.             output.append(sizeof(char), currentLeft[i]);
  59.             currentLeft.erase(currentLeft.begin() + 0);
  60.             output.append(sizeof(char), currentLeft[i]);
  61.             currentLeft.erase(currentLeft.begin() + 0);
  62.             output.append(sizeof(char), currentRight[i]);
  63.             currentRight.erase(currentRight.begin() + 0);
  64.             i = 0;
  65.         }
  66.         return output;
  67.     }
  68.     else
  69.     {
  70.         string currentLeft = left;
  71.         string currentRight = right;
  72.  
  73.         for(size_t i = 0; i < leftSize + rightSize;)
  74.         {
  75.             if(currentLeft.empty())
  76.             {
  77.                 output.append(currentRight);
  78.                 break;
  79.             }
  80.             if(currentRight.empty())
  81.             {
  82.                 output.append(currentLeft);
  83.                 break;
  84.             }
  85.             output.append(sizeof(char), currentLeft[i]);
  86.             currentLeft.erase(currentLeft.begin() + 0);
  87.             output.append(sizeof(char), currentRight[i]);
  88.             currentRight.erase(currentRight.begin() + 0);
  89.             output.append(sizeof(char), currentRight[i]);
  90.             currentRight.erase(currentRight.begin() + 0);
  91.             i = 0;
  92.         }
  93.         return output;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement