5.3.c 628 B

12345678910111213141516171819202122232425262728293031323334
  1. /* strcat: concatenate t to end of s */
  2. #include <stdio.h>
  3. void strcat(char s[], char t[]);
  4. int main()
  5. {
  6. char lol1[100] = "aaaa";
  7. char lol2[100] = "xxxx";
  8. printf("string 1 : %s \n",lol1);
  9. printf("string 2 : %s \n",lol2);
  10. strcat(lol1,lol2);
  11. printf("string 1 : %s \n",lol1);
  12. printf("string 2 : %s \n",lol2);
  13. return 0;
  14. }
  15. /* write a pointer version of the function strcat that we showed in Chapter 2:
  16. strcat(s,t) copies the string t to the end of s. */
  17. void strcat(char s[], char t[])
  18. {
  19. // seek to end of s
  20. char* sp=s;
  21. while (*(sp++));
  22. sp--;
  23. do
  24. {
  25. (*sp)=(*t);
  26. sp++;
  27. }
  28. while (*(t++));
  29. }