![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 31
| Another String Problem Please just point me in the direction of how to fix what I am doing wrong if you can. I am working on a project for class to reverse a string. I have already written the iteration version of the function and it works great. However, I now have to write it using recursion and I am stuck! I know how to simply use recursion to print out the string in reverse, but we need to actually reverse the string in memory using recursion not just print it in reverse. Here is the code I have so far: Code: //prototypes
void Reverse(char *ptrA);
void RecursiveReverse(char *ptrA);
//call in main
Reverse(Array1);
RecursiveReverse(Array2);
/* Reverse the contents of a char array
Input: a pointer to a char array
Output: the contents of the array in reverse order
*/
void Reverse(char *ptrA)
{
char temp;
char *pStart = ptrA;
char *pEnd = ptrA;
while ( *pEnd != NULL) // assume char *pEnd ends with '\0'
{
pEnd ++; // count the values in the array
}
pEnd--; // determine the proper length of the array because indexes start at zero
while ( pStart < pEnd ) // this loop swaps the values and reverses the array
{
temp = *pStart;
*pStart = *pEnd;
*pEnd = temp;
pStart ++;
pEnd--;
}
}
/* Reverse the contents of a char array using recusion
Input: a pointer to a char array
Output: the contents of the array in reverse order
*/
void RecursiveReverse(char *ptrA)
{
char temp;
int size;
char *pStart;
char *pEnd;
pStart = ptrA;
size = strlen(pStart);
pEnd = pStart + (strlen(pStart)-1);
if(pStart >= pEnd) /* base case */
{
return;
}
else //we want to swap
{
temp = *pStart;
*pStart = *pEnd;
*pEnd = temp;
pStart = pStart + 1;
RecursiveReverse(pStart); /* recursive call */
}
}
Right now, it is only moving the last value in the char array to the front. All Help is appreciated. |
| w2look is offline | |
| | #2 |
| Registered User Join Date: Nov 2008
Posts: 31
| A little more Info This is what the constraints of the project are: Write a function called Reverse that accepts as a parameter a char array (or char pointer) and returns a void. The function reverses the contents of the array (or string) being passed. Example: the string "Hello" will be reversed to "olleH". You have to be careful about the '\0' symbol to keep it at the end of the array and watch out for odd and even length strings. Test your function and display the string (array) before and after it is reversed. Repeat this function using recursion. Name your function RecursiveReverse. IMPORTANT: The function does NOT print the string in reverse, it actually reverses it in memory. |
| w2look is offline | |
| | #3 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| As a first step spend time in preparing the algorithm. Once you have the algorithm down, coding the recursive function becomes a much easier task. |
| itCbitC is offline | |
| | #4 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Hint: store the original size of the string in a local variable that retains its value between function calls. |
| itCbitC is offline | |
| | #5 |
| Registered User Join Date: Nov 2008
Posts: 31
| Sometimes you just have to walk away from the computer and clear your head. The solution came to me as I was cooking breakfast this morning. You have to pass it two pointers. One to the start and one to the end. Do the swap, increment the start, decrement the end and pass them back. Code: /* prototypes */
void RecursiveReverse(char *ptrA, char *ptrB);
//call in main
RecursiveReverse(Array2, (Array2 + (strlen(Array2) - 1)));
/* Reverse the contents of a char array using recusion
Input: a pointer to a char array and a pointer to the end of that array
Output: the contents of the array in reverse order
*/
void RecursiveReverse(char *ptrA, char *ptrB)
{
char temp; //needed for the swap
char *pStart; //pointer to start of array
char *pEnd; //pointer to end of array
pStart = ptrA;
pEnd = ptrB;
if(pStart >= pEnd) // base_case if the pointers meet or overlap we're done
{
return;
}
else //we want to swap
{
temp = *pStart; //perform the swap
*pStart = *pEnd;
*pEnd = temp;
pStart++; //increment the start position
pEnd--; //decrement the end position
RecursiveReverse(pStart, pEnd); //recursive call with the new pointer values
}
}
|
| w2look is offline | |
| | #6 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,359
| That is good. Now, simplify it. Note that you never use ptrA and ptrB in the function other than to assign their values to pStart and pEnd. Perhaps you can do with just one pair of pointers? Instead of the if block in which you return, could you just have an if block in which you perform the swap and recursive call? Instead of incrementing pStart and decrementing pEnd, could you just call RecursiveReverse() with the appropriate arguments?
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
| | #7 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| |
| itCbitC is offline | |
![]() |
| Tags |
| functions, recursion, reverse, strings |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Inheritance Hierarchy] User Input on program with constructors. How ? | chandreu | C++ Programming | 8 | 04-25-2008 02:45 PM |
| String issues | The_professor | C++ Programming | 7 | 06-12-2007 09:11 AM |
| Custom String class gives problem with another prog. | I BLcK I | C++ Programming | 1 | 12-18-2006 03:40 AM |
| Compile Error that i dont understand | bobthebullet990 | C++ Programming | 5 | 05-05-2006 09:19 AM |
| String editor for a sentence inputted by a user - any suggestions or ideas? | the_newbug | C Programming | 4 | 03-03-2006 02:11 AM |