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;
	}
}

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;

Edited 6 Years Ago by hag++: forgot something

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.

Edited 6 Years Ago by thomas_naveen: error

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

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

that fixed the artifact but now my output is printing vertical

This question has already been answered. Start a new discussion instead.