C Program to Concatenate Two Strings Using Pointers



Character array is called string. The last character is always null in a string represented by \0. The indexing always start from 0 in any array.
In the following program pointers are used to copy the value of bb string to aa string



  1. //C program to concatenate strings using pointers
  2. #include <stdio.h>
  3. int main()
  4. {
  5. char aa[100], bb[100]; //array declaration
  6. printf("\nThe string after concatenation is: %s ", aa);
  7. printf("\nEnter the first string: ");
  8. gets(aa); //using gets allows us to take string inputs along with spaces
  9. printf("\nEnter the second string to be concatenated: ");
  10. gets(bb);
  11. char *a = aa;
  12. char *b = bb;
  13. while(*a)
  14. {
  15. a++;
  16. }
  17. while(*b)
  18. {
  19. *a = *b;
  20. b++;
  21. a++;
  22. }
  23. *a = &#x27;\0&#x27;; // string must end with &#x27;\0&#x27;
  24. printf("\n\n\nThe string after concatenation is: %s ", aa);
  25. return 0;
  26. }

Output
Output!
download android app

Popular C Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube