help me figure this out

i done all this just stumped on doing the backwardstring i know it has to look like getthestring part but in reverse please help me out

// 1. Length of a C-String
int stringlength(char charray[])
{
int length = 0;
// Get the data
// the character string will be passed to function as a parameter
// Determine the length of the C-String
// Use a loop to iterate over the elements and look for '\0'
do {
if ( charray[ length ] == '\0' )
break;
++length;
} while (true);

return length;

}

//2. Backward String
int getthestring( char charray[], int maxsize)
{
int length = 0;
cout << "please enter a single word less than" << maxsize;
cout << "characters in length\n";
cin >> charray;
length = stringlength (charray);
return length;
}
int backwardstring()
{

}

void backwardstring()
{
char charray[256] = {'\0'};
int arrsize = 0;


//2.1 get the data: a function that prompts user for a string
getthestring( charray, 256 - 1);
//2.2 use the data: create a string of backward characters
reversethestring( charray, 256 - 1);
//2.3 output the data: send the backward to the standard output
cout << charray << endl;
}
Last edited on
I'm guessing using the string class is out of the question, by the may you've specifically mentioned C-style strings.

There's a reasonably easy algorithm for reversing them, though.

You'll need three variables:
- int to store the length of the string
- int to store the last position, initialise to length - 1
- char for temporary operations

To do it:
- Loop from 0 to length / 2 (let's call loop index i)
- Store the ith character in the string in temp char
- Store the character of the start at (last - i) in the current ith position of the string
- Store what is currently in temp in the (last - i) position of the string

That's it.

I'm happy to elaborate further and I appreciate the pseudo looks a bit messy. I think it's important that you understand why it works and write the code out for yourself, though.
Topic archived. No new replies allowed.