I'm doing a program that is supposed to reverse a string entered my the user using a recursive function, I almost have it solved except for one little elusive bug that I can not understand.

//******************************************************************************
//Programer: Kyle Willett
//Course:    CS-1513
//Program:   3 exercise 7 page 933.
//Purpose:   Reverse the contents of a string.
//Input:     A string.
//Calculate: The reverse of the inputed string.
//Output:    The user intputed string, and the reversed string.
//******************************************************************************

#include<iostream>
#include<string>
#include<cstdlib>

using namespace std;

//******************************************************************************
//function prototypes
void input(string &stringInput, int &size);
//This function gets a string from the user.

void fillReverse(string &reversed, int size);
//This function was not part of the original design, it proved neccesary because
//the reversed string in main would not work in the reverse function unless it
//had been initilized to some value, and becuse it is not know what size the 
//user string is until entered the reversed string cann't be intitilized in main
//so this function is used to fill it with random values for the same size of
//the user entered string.

void output(string userStringOut, string reversedStringOut);
//This function outputs the user string and the reversed string.

void reverseString(string userString, string &reversedString, int size);
//This function recursivly calculates the reverse of a string.

//******************************************************************************
//******************************************************************************
int main()
{
    string userString;
    string reversed;
    //The two string variables for the reversing operation.
    
    cout << endl << reversed << endl;
    
    int size=0;
    
    input(userString, size);
    
    fillReverse(reversed ,size);
    
    reverseString(userString, reversed, size-1);
    
    output(userString, reversed);
    
    return 0;
}
//******************************************************************************
//******************************************************************************
//function implimentations

void input(string &stringInput, int &size)
{
    cout << "Please enter a string you would like to see reversed." << endl;
    cout << "Enter a string: ";
    cin >> stringInput;
    cout << endl << endl;
    
    size=stringInput.length();
    //This function gets the length of the string for use in the
    //recursive function.
}
//******************************************************************************
void output(string userStringOut, string reversedStringOut)
{
    cout << "The contents of the string you entered is: ";
    cout << userStringOut;
    cout << endl;
    cout << "The contensts of the reversed string is: ";
    cout << reversedStringOut;
    cout << endl << endl;
}
//******************************************************************************
void reverseString(string userString, string &reversedString, int size)
{
    char temp;
    static int n;
    
    if(size >= 0)
    {
        temp=userString[n];
        reversedString[size]=temp;
        n++;
        reverseString(userString, reversedString, size-1);
    }
}
//******************************************************************************
void fillReverse(string &reversed, int size)
{
    char fillLetter=65;
    
    for(int n=0; n< size; n++)
    {
        reversed[n]=fillLetter;
    }
}
//******************************************************************************

On line 41 if I supply a stating value for the reversed string that is the same size as the one I input for example "AAAA" and I input "test" the program outputs "tset" which is correct, but as it is with no starting value for the reversed string the program doesn't output a value for the reversed string. To solve this I tried to write the fillReverse function but this didn't solve the problem.

I tried running this program on my local windows xp computer from the command line and the school I go to student unix server and got the same result.

Any help on why the string has to have an initial value and ways to solve the problem would be appreciated. Thanks.

Hmm, I'd say that a good solution is to rewrite your reverseString() entirely so that it builds the reversed string rather than tries to fill a caller provided object:

#include <iostream>
#include <string>

using namespace std;

string reverseString(string::const_iterator first, string::const_iterator last)
{
    if (first == last)
        return string();

    return reverseString(first + 1, last) + *first;
}

int main()
{
    string userString;

    cout << "Please enter a string you would like to see reversed." << '\n';
    cout << "Enter a string: ";
    
    if (getline(cin, userString)) {
        string reversed = reverseString(userString.begin(), userString.end());
        
        cout << "The contents of the string you entered is: " << userString << '\n';
        cout << "The contensts of the reversed string is: " << reversed << '\n';
    }
}

Greatly simplified, no?

Thanks for the help. though I kept on working on solving it and eventially came up with the solution using the line

reversed=userString;

in side the main function and remove the fillReverse function. This way the reverse array is already the correct size when it reaches the reverseString function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.