Write C program to concatenate two strings using pointer


Introduction

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Write C program to concatenate two strings using pointer


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

Result

C language techstudy.org

     





Related Topics




Popular Posts