//========================================================================= #define PROGRAMMER "SOLUTIONS, NoFrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2003) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "HW #6C" #define REVISION (0) #define TITLE "The Mysterious Disappearing String" #define SUBTITLE "Extra Credit" #define EMAIL "wbahn@eas.uccs.edu" #define FILENAME "mystery.c" //========================================================================= // PROBLEM: // // The following program appears not to work properly. Of course, it is // doing precisely what it has been told to do, but it's behavior is not // obvious. // // So what "should" it do? It should be a loop that sets a string pointer // to point to a string and then removes the last character by replacing // replacing it with a null terminator. // // But since we set the string pointer to point to the original string // each and every time through the loop, each pass through the loop should // produce identical results (the original string with the last character // removed). // // Why doesn't it? If you can adequately explain why this program behaves // the way it does, you can extra credit on this homework assignment. Your // explanation should be written as though a student came to you with this // problem and you were charged with explaining it to them in sufficient // detail so that they understand what it happening. // // SOLUTION: // // PLACE YOUR EXPLANATION HERE. // SUBMIT THIS FILE AS YOUR HW#6C SUBMISSION. // DON"T FORGET TO MODIFY THE HEADER AND NAME THE FILE APPROPRIATELY. //== INCLUDE FILES ======================================================== #include // printf() #include // strlen() //== TOP LEVEL SUPPORE FUNCTION PROTOTYPES ================================ void PrintHeader(void); //== MAIN FUNCTION ======================================================== int main(void) { char *fred; int i; PrintHeader(); printf("\n"); // Print a blank line do { fred = "The mysterious disappearing string."; printf("%s\n", fred); i = strlen(fred); fred[i-1] = '\0'; } while(i>0); return(0); } //== SUPPORT FUNCTIONS ==================================================== void PrintHeader(void) { printf("========================================" "=======================================\n"); printf("Course....... %s-%i (%s %i)\n", COURSE, SECTION, TERM, YEAR); printf("Programmer... %s (%s)\n", PROGRAMMER, PROG_CODE); printf("Assignment... %s (Rev %i) (Source Code in %s)\n", ASSIGNMENT, REVISION, FILENAME); printf("Description.. %s\n", TITLE); printf(" %s\n", SUBTITLE); printf("========================================" "=======================================\n"); return; }