Write C++ program to concatenate two strings using pointer


I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. #define MAX_SIZE 100 // Maximum size of the string
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7. char str1[MAX_SIZE], str2[MAX_SIZE];
  8. char * s1 = str1;
  9. char * s2 = str2;
  10.  
  11. // Inputting 2 strings from user
  12. cout<<"Enter 1st string: ";
  13. cin>>str1;
  14. cout<<"Enter 2nd string: ";
  15. cin>>str2;
  16.  
  17. // Moving till the end of str1
  18. while(*(++s1));
  19.  
  20. // Coping str2 to str1
  21. while(*(s1++) = *(s2++));
  22.  
  23. cout<<"Concatenated string:"<<str1;
  24.  
  25. return 0;
  26.  
  27. }
  28.  

Result

C++ language techstudy.org

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.