waltermilner

Untitled

Jul 15th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h> // for output
  2. #include <string.h> // string functions
  3. #include <stdlib.h> // standard library
  4.  
  5. // In C, strings are stored as character codes ( ASCII or Unicode )
  6. // and have a 0 at the end
  7.  
  8. int main () // execution starts here
  9. {
  10.     char * str = "ABCDEFG";             // string in memory. Has 0 at end
  11.     const int BUFSIZE=256;              // size of buffer = 256 bytes
  12.     char * buffer = malloc(BUFSIZE);    // get memory block of this size
  13.     for (int i=strlen(str); i>0; i--)   // for loop. i decrease from string length to 0
  14.     {
  15.         memset(buffer, 0,BUFSIZE);      // fill buffer with zero
  16.         strncpy(buffer,str, i);         // copy i bytes from str to buffer
  17.         printf("%s\n", buffer);         // print buffer
  18.     }
  19.     return 0;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment