working on the backwards String and i can't get it to print correctly plz help

#include <iostream>

using namespace std;

void backward(char *);	// Function prototype

int main()
{
	const int SIZE = 11;	// Array size
	char string[SIZE];		// To hold a string

	// Ask the user to enter a string.
	cout << "Enter a string (up to 10 characters) ";
	cout << "and I will reverse it: ";
	cin.getline(string, SIZE);

	// Display the number of characters in the string.
	cout << "Written backward, your string is: ";
	backward(string);
	return 0;
}

//*****************************
// Definition of stringLength.*
//*****************************

void backward(char * strPtr)
{
	const int SIZE = 11;
	int count;		// Loop counter
	
	for(count = SIZE; count >= 0; count--);
	{
		cout << strPtr[count] << endl;
	}
}

Can you copy the output of your program here?

all i get for output is |}

What was the input string?

the input string is hello

Looking at the for loop in your backwards function:

for(count = SIZE; count >= 0; count--);	{		cout << strPtr[count] << endl;	}

The first thing you print is strPtr[11] which will contain logical garbage, the array will only go to subscript [10] if the array size is 11.
Instead, what about this:

for (count = strlen(strPtr); count >= 0; count --)
                  cout << strPtr[count] << endl;
for(count = SIZE; count >= 0; count--);

Remove the ';' at the end of the for. You are going to get some junk characters anyway since the size of the array is 11 and the string that you are passing is less than 11 characters.

To avoid this you could use some standard string length function to get the length of the string.

@hag++ Apologies. Didn't see your post.

yep, put thomas' and my posts together and you got yourself a correct answer haha

so how do i fix that

ahhh ok i see now thx i'll get right on that

now my output is vertical but other than that it works great

that fixed the artifact but now my output is printing vertical

Try removing the endl.

Otherwise copy the reversed string to a new string and print.

thx guys you were a big help that got it working

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.