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 = '\0'; // string must end with '\0'
  24. printf("\n\n\nThe string after concatenation is: %s ", aa);
  25. return 0;
  26. }

Output

Output!