Shaco74

cppisgae

Jan 6th, 2022 (edited)
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int get_length(const char *ptr)
  6. {
  7.   for (int idx = 0; ptr[idx] != '\0'; idx++)
  8.   {
  9.     if (ptr[idx + 1] == '\0')
  10.       return idx + 1;
  11.   }
  12.   return -1;
  13. }
  14.  
  15. char *my_strconcat(const char *ptr1, const char *ptr2)
  16. {
  17.   int len = get_length(ptr1) + get_length(ptr2);
  18.   char *writable = new char[len + 1];
  19.   writable[len] = '\0';
  20.  
  21.   bool check = false;
  22.  
  23.   for (int idx = 0; idx < len; idx++)
  24.   {
  25.     if (ptr1[idx] == '\0')
  26.     {
  27.       check = true;
  28.     }
  29.     if (check)
  30.     {
  31.       writable[idx] = ptr2[idx - get_length(ptr1)];
  32.       continue;
  33.     }
  34.     writable[idx] = ptr1[idx];
  35.   }
  36.  
  37.   return writable;
  38. }
  39.  
  40. char *convert_string(string pString)
  41. {
  42.   char *writable = new char[pString.size() + 1];
  43.   int le = pString.size();
  44.   writable[le] = '\0';
  45.  
  46.   for (int idx = 0; idx < le + 1; idx++)
  47.   {
  48.     writable[idx] = pString[idx];
  49.   }
  50.  
  51.   return writable;
  52. }
  53.  
  54. int main()
  55. {
  56.   string in1;
  57.   string in2;
  58.   char *out;
  59.   cout << "Bitte ersten Text eingeben (ggfs. mit Leerzeichen): ? ";
  60.   getline(cin, in1);
  61.   cout << "Bitte zweiten Text eingeben (ggfs. mit Leerzeichen): ? ";
  62.   getline(cin, in2);
  63.  
  64.   if (in1 != "" && in2 != "")
  65.   {
  66.  
  67.     char *cstr1 = convert_string(in1);
  68.     char *cstr2 = convert_string(in2);
  69.  
  70.     out = my_strconcat(cstr1, cstr2);
  71.  
  72.     cout << "Ergebnis my_strconcat(): " << out << endl;
  73.   }
  74.   else
  75.   {
  76.     cout << "Ergebnis my_strconcat(): " << endl;
  77.   }
  78.  
  79.   system("PAUSE");
  80.   return 0;
  81. }
Add Comment
Please, Sign In to add comment