File: Stare vježbe/vjezbe09/58__strcpy.c

  1. /*
  2.   58__strcpy.c
  3.   Primjeri implementacije funkcije strcpy.
  4.   -----
  5.   Prototip: void strcpy(char *s, char *t);
  6.   Funkcija strcpy kopira kompletni sadrzaj stringa t (ukljucujuci i
  7.   posljednji '\0' karakter) na adresu gdje se nalazi string s.
  8. */
  9.  
  10. #include <stdio.h>
  11.  
  12. #define MAX 50
  13.  
  14. void strcpy1(char *s, char *t) {
  15. int i=0;
  16. while((s[i]=t[i])!='\0')
  17. i++;
  18. }
  19.  
  20. void strcpy2(char *s, char *t) {
  21. while((*s=*t)!='\0')
  22. s++, t++;
  23. }
  24.  
  25. void strcpy3(char *s, char *t) {
  26. while((*s++=*t++)!='\0');
  27. }
  28.  
  29. void strcpy4(char *s, char *t) {
  30. while(*s++=*t++);
  31. }
  32.  
  33. int main() {
  34. char s1[MAX], s2[MAX];
  35.  
  36. gets(s1);
  37. printf("\ns1: %s\n", s1);
  38.  
  39. strcpy1(s2, s1);
  40. printf("strcpy1(s2, s1) -> s2: %s\n", s2);
  41. strcpy2(s2, s1);
  42. printf("strcpy1(s2, s1) -> s2: %s\n", s2);
  43. strcpy3(s2, s1);
  44. printf("strcpy1(s2, s1) -> s2: %s\n", s2);
  45. strcpy4(s2, s1);
  46. printf("strcpy1(s2, s1) -> s2: %s\n", s2);
  47.  
  48. return 0;
  49. }
  50.