File: Stare vježbe/vjezbe09/59__strcat.c

  1. /*
  2.   59__strcat.c
  3.   Primjeri implementacije funkcije strcat.
  4.   -----
  5.   Prototip: void strcat(char *s, char *t);
  6.   Funkcija strcat sluzi za konkatenaciju dvaju stringova, odnosno
  7.   dodaje sadrzaj stringa t na kraj stringa s.
  8. */
  9.  
  10. #include <stdio.h>
  11.  
  12. #define MAX 100
  13.  
  14. void strcat1(char *s, char* t) {
  15. int i=0, j=0;
  16.  
  17. /* trazimo kraj stringa s */
  18. while(s[i]!='\0')
  19. i++;
  20. /* dodajemo string t na string s */
  21. while(t[j]!='\0')
  22. s[i++]=t[j++];
  23. /* dodajemo '\0' na kraj stringa s */
  24. s[i]=t[j];
  25. }
  26.  
  27. void strcat2(char *s, char* t) {
  28. while(*s)
  29. s++;
  30. while(*s=*t)
  31. s++, t++;
  32. }
  33.  
  34. int strlen3(char* s) {
  35. char* p=s;
  36. while(*p)
  37. p++;
  38. return p-s;
  39. }
  40.  
  41. void strcat3(char* s,char*t) {
  42. int i=strlen3(s);
  43. while(s[i++]=*t++);
  44. }
  45.  
  46. int main() {
  47. char s1[MAX], s2[MAX];
  48.  
  49. gets(s1);
  50. gets(s2);
  51. printf("\ns1: %s s2: %s\n", s1, s2);
  52.  
  53. strcat1(s2, s1);
  54. printf("strcat1(s2, s1) -> s2: %s\n", s2);
  55. strcat2(s2, s1);
  56. printf("strcat2(s2, s1) -> s2: %s\n", s2);
  57. strcat3(s2, s1);
  58. printf("strcat3(s2, s1) -> s2: %s\n", s2);
  59.  
  60. return 0;
  61. }
  62.