Advertisement
trenka

Untitled

Feb 25th, 2021
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. sh-5.0$ cat 1.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define MAXLEN 100
  6.  
  7. void strcat2(char *destination, char *source);
  8.  
  9. int main()
  10. {
  11.     // create a character array. All of these C strings are terminated with a '\0'
  12.     char start[MAXLEN] = "This is a sentence looking for";
  13.     char end[] = " an ending!";
  14.  
  15.     // strcat2 destination, source
  16.     strcat2(start, end);
  17.  
  18.     // str is the same as &str[0] which is handed to printf and prints all the
  19.     // up to '\0'
  20.     printf("%s\n", start);
  21. }
  22.  
  23. void strcat2(char *start, char *end)
  24. {
  25.     // move pointer to end of start.
  26.     while (*start++)
  27.         ;
  28.    
  29.     // allows me to overwrite the null character
  30.     start--;
  31.  
  32.     while ((*start++ = *end++))
  33.         ;
  34. }
  35. sh-5.0$ gcc 1.c
  36. sh-5.0$ ./a.out
  37. This is a sentence looking for an ending!
  38. sh-5.0$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement