please help

OK SIMPLE CODE WITH AN ARRAY THAT I NEED TO DECLARE THE SIZE TO 16 AND OUT PUT A-P FORWARD AND BACKWARDS IN CAN ONLY GET IT TO WORK IF I DECLARE IT TO SIZE 17 BUT WONT WORK IF SIZE IS 16?????? I NEED IT TO WORK WITH SIZE 16. WHAT AM I DOING WRONG???

#include<iostream>

using namespace std;
void reverse_string(char*);
int main()

{
const int size=16;
char myarr[size]="abcdefghijklmnop"; // THIS IS WHERE MY PROBLEM IS


cout << "\nthe forward array is:\n\n"<< myarr;
cout <<"\n\n\n";

reverse_string(myarr);
cout << "the array in reverse is:"<< "\n\n";
cout << myarr<<"\n\n";

cout <<"\n\n\n";
system("pause");
return 0;
}

// reverse string function
void reverse_string(char* ch_ptr )
{
char* front;
char* end;
char temp;

front=end=ch_ptr;

while (*end != '\0')
++end;
--end;

while(front<end)
{
temp=*front;
*front=*end;
*end=temp;
++front;
--end;
}
return ;
}
With character arrays you need space for a null character at the end. I pulled this example from the tutorial page on this site.

http://www.cplusplus.com/doc/tutorial/ntcs/
Sequences of characters enclosed in double-quotes (") are literal constants. And their type is, in fact, a null-terminated array of characters. This means that string literals always have a null character ('\0') automatically appended at the end.

Therefore, the array of char elements called myword can be initialized with a null-terminated sequence of characters by either one of these two statements:

1
2
char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char myword[] = "Hello"; 


In both cases, the array of characters myword is declared with a size of 6 elements of type char: the 5 characters that compose the word "Hello", plus a final null character ('\0'), which specifies the end of the sequence and that, in the second case, when using double quotes (") it is appended automatically.

use this;
...
string myarr[size]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"};
that dont work, it has to be an array with size at 16.
string myarr[size]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"}; this does not work on my program, i need the array to be size 16, i know that you need the space to be a null '\0', but the experiment calls for the array to output A-P with the size 16, im stuck!!!
Perhaps he meant
char myarr[size]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'};

However, your reverse string function depends on finding a null character in your array.
Last edited on
Hi,

If your assignment really wants you to make the array have a size of 16 elements, then to print it out, you'll have to use a for loop like:

for(int counter = 0; counter < 16; counter++)
{
cout<<myarr[counter];
}

When you just do this: cout<<myarr;
What you're actually doing is passing in the ADDRESS of the array and the extraction operator will print all the data from that starting address until it reaches a null character (\0). However, since you're array cannot have a null character, you must use a for loop instead to print out the array. Without the null character, you'll go past the edge of the array and cause a runtime error.

You can email me at: sparkprogrammer@gmail.com

Joe
Last edited on
Do you just have to output the letters in reverse order or do you have to actually reverse the letters in the array itself?

If you just had to output in reverse, you could use another for loop and start at the end of the array.
1
2
3
	cout << "the array in reverse is:" ;
	for (int i=15;i>=0;i--)
		cout << myarr[i];
i have to do both forward and reverse, which i did but my issue is not the reverse, it is the array being filled by A-P and the size HAS to be 16. i think i need a for() loop to output the A-P.


and to everyone else who posted back, thanks for the info it was great.
Topic archived. No new replies allowed.