Advertisement
krstoilo

Concatenate C-strings

Feb 11th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void concatStrings(char target[], const char source[]){
  6.  
  7. int targetIndex = 0;
  8.  
  9. while(target[targetIndex]){ /// while it's not the "\0" continue the loop,
  10.                             /// otherwise break
  11.  
  12.     targetIndex++;
  13.  
  14. }
  15.  
  16. int sourceIndex = 0;
  17.  
  18. while(source[sourceIndex]){
  19.  
  20.     target[targetIndex] = source[sourceIndex];
  21.     targetIndex++;
  22.     sourceIndex++;
  23.  
  24. }
  25.  
  26.     target[targetIndex] = '\0';
  27.  
  28.     cout << "Size of target : " << targetIndex << endl;
  29.     cout << "Size of source : " << sourceIndex << endl;
  30.  
  31. }
  32.  
  33.  
  34. int main(){
  35.  
  36.     char string1[256];
  37.     cin.getline(string1,128);
  38.  
  39.     char string2[128];
  40.     cin.getline(string2, 128);
  41.  
  42.     concatStrings(string1, "-");
  43.     concatStrings(string1,string2);
  44.  
  45.     cout << "\n" << string1 << endl;
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement