Mega Code Archive

 
Categories / C / Code Snippets
 

Allocate space for a string dynamically

request user input, and then print the string backwards #include <stdlib.h> #include <stdio.h> #include <string.h> int main(void) { char *j; register int i; j = malloc(80); if(!j) { printf("Memory request failed.\n"); exit(1); } gets(j); for(i = strlen(j) - 1; i >= 0; i--) putchar(j[ i ]); free(j); return 0; }