I am trying to write this program to reverse a string. If I were to give it the string "birds and bees," it would give me bees and birds in response. This program does not work, though, and results in a segmentation fault. What is my error and how do I fix it?Code:#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { char* original = (char*) malloc(80 * sizeof(char)); char reverse[80]; char temp[80]; //same as char* temp char *ptr; printf("Input: "); fgets(original, 80, stdin); ptr = strtok(original, " "); while(strtok(original, " \n") != NULL) //until end of line { ptr = strtok(NULL, " \n"); sprintf("%s", ptr, reverse); //THIS CAUSES THE ERROR strcpy(reverse, temp); }//while printf("%s", reverse); free(original); return 0; }//int main(void)
Thank you.



LinkBack URL
About LinkBacks


