/* Write a function that takes a C string as an input parameter and reverses the string. The function should use two local pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the preceding character, and so on, until the entire string is reversed. Write a main program to test your function on various strings of both even and odd length. */ #include #include using namespace::std; void reverse(char *); void main() { // RUN YOUR PROGRAM AT LEAST 3 TIMES TO TEST // A C++ string with exactly 1 char // A C++ string with an even number of characters // A C++ string with an odd number of characters // COPY AND PASTE THE OUTPUT OF EACH RUN AS A COMMENT // AT THE END OF THIS FILE const int SIZE = 101; // declare a char array of 101 characters char charArray[SIZE]; cout << "This programs takes a C string as input, reverses characters,\n" << "then prints the result.\n"; // prompt the user to enter a char array with no spaces cout << "Enter characters from keyboard, except space or line breaks.\n"; cout << "Press Enter when complete. >" << SIZE - 1 << " characters are truncated.\n"; cin >> charArray; // Ensure last index is null value for C-string charArray[SIZE - 1] = '\0'; // echo the data the user typed in cout << "\nYou entered:\n" << charArray << endl; // call the reverse function reverse(charArray); // display the char array after the function reverse has been called cout << "\nYour input reversed:\n" << charArray << endl; return; } // ====================== // reverse: // Reverses the input string using pointers. // ====================== void reverse(char *s) { // write the code for the reverse function using pointers // you cannot use array notation char *front, *rear, *tempThenNull; int cStringSize = strlen(s); front = s; rear = s + (cStringSize - 1); // NOTE: s+(cStringSize) is '\0' value. tempThenNull = s + (cStringSize); // temporary pointer to NULL '\0' that I'll clobber then reset for (int i = 0; i < (cStringSize/2); i++) { *tempThenNull = *(front + i); *(front + i) = *(rear - i); *(rear - i) = *tempThenNull; // Line of code for testing only. //cout << "*(front+" << i << ")=" << *(front+i) << " *(rear+" << i << ")=" << *(rear+i) << endl; } // reset temporary pointer back to NULL *tempThenNull = '\0'; return; } /* ////////////////// INSTRUCTOR reverse() /////////////// void reverse(char *s) { char *front, *rear, *count; front = s; count = s; while (*count != '\0') { count++; } rear = count - 1; while(front < rear) { char holder; holder = *front; *front = *rear; *rear = holder; front++; rear--; } } */ /* Output ****** * INPUT 1 char ***** This programs takes a C string as input, reverses characters, then prints the result. Enter characters from keyboard, except space or line breaks. Press Enter when complete. >100 characters are truncated. z You entered: z Your input reversed: z Press any key to continue . . . ***** * INPUT even number of characters ***** This programs takes a C string as input, reverses characters, then prints the result. Enter characters from keyboard, except space or line breaks. Press Enter when complete. >100 characters are truncated. abcdefghijklmnopqrstuvwxyz You entered: abcdefghijklmnopqrstuvwxyz Your input reversed: zyxwvutsrqponmlkjihgfedcba Press any key to continue . . . ***** * INPUT odd number of characters ***** This programs takes a C string as input, reverses characters, then prints the result. Enter characters from keyboard, except space or line breaks. Press Enter when complete. >100 characters are truncated. abcde123456 You entered: abcde123456 Your input reversed: 654321edcba Press any key to continue . . . */