/* Grant Chen Due: 10/16/08 CS213 Student ID 908306235 Compiler: Dev-C++ Exercise 5 Problem Description 10.3 Write a program that accepts a C-string input from the user and reverses the contents of the string. Your program should work by using two pointers. The “head” pointer should be set to the address of the first character in the string, and the “tail” pointer should be set to the address of the last character in the string (i.e., the character before the terminating null). The program should swap the characters referenced by these pointers, increment “head” to point to the next character, decrement “tail” to point to the second-to-last character, and so on, until all characters have been swapped and the entire string reversed. Submission instructions For this homework, please implement it by a single .cpp file, i.e., no separate compilation needed. Ideas: include a loop that goes on until the head EQUALS the tail Take in a c-string Make a pointer to the [0]. Make a pointer to the [length minus 1] Until the pointers are equal, swap the contents of the things Pseudocode: Take in c-string input Get stringlength Create a pointer at 0. Create a poitner at stringlength. Create a loop. Swap the values of 0 and stringlength. Increment 0 and decrement stringlength pointer. Swap the next Keep going until they both point at the same address, or the old stringlength is pointing at a smaller element than the old 0. */ #include using namespace std; main() { char user_input[100]; // Initialize c-string char *head, *tail; // Initialize pointersc char placeholder; // placeholder int stringlength; // Initialize string length int i; // Used for the while loop cin.getline(user_input, 100); // User inputs line cout << user_input << endl;; // This is a test line to output the c-string. for (stringlength = 0; user_input[stringlength]; stringlength++) { } // This loop finds the length of the string. cout << stringlength << endl;// This outputs the length of the string. head = &user_input[0]; // Setting head to the first character. tail = &user_input[stringlength-1]; // Setting tail to the last character. cout << "head: " << *head << endl; // Testing head value. cout << "tail: " << *tail << endl; // Testing tail value. // Now for the rough part. while (i != (stringlength-1)/2) { cout << "taking head" << endl; head = &user_input[i]; cout << "taking tail" << endl; tail = &user_input[stringlength + 1 - i]; cout << "head into placeholder" << endl; placeholder = *head; cout << "tail into head" << endl; *head = *tail; // THIS IS WHERE THE PROGRAM FAILS. cout << "placeholder into tail" << endl; *tail = placeholder; // THERE IS ANOTHER CRASH HERE. cout << "incrementing i" << endl; i++; }; cout << user_input; return 0; }