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

First thing - STOP SHOUTING!

Next, always use code tags.

char arrays have a null to delimit the end of the array.

One needs to allow for this when declaring & initialising the variable.

EDIT:

The thing is you have this concept on this line:

while (*end != '\0')

but I am not sure what you are trying to do with the lines that follow.
Last edited on
what? i know the array has a null zero that's why if i make the size 17 it will work. but im having trouble doing this with size=16. i think i need a for loop to initialize the array A-P, but what ive tried just didn't work. or i could just be wrong all together. i dont know how to do this. and im not sure what a code tag is.
Last edited on
Ok so many chars are trying to put in the array? Count them.

For using various types of tags:

http://www.cplusplus.com/articles/z13hAqkS/
what im trying to do is out put an array A-P forward and then output that same array backwards, with the array at size 16.

while (*end != '\0'), this would only apply to my function that shows the output in revers.
If you really want/need to cram it into an array of size 16, I suppose you could do this:
1
2
const int size = 16;
char myarr[size] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'};
But then you wouldn't be able to do std::cout << myarr; or anything that assumes there's a null character at the end of the string.

So it would have to be
1
2
3
cout << "\nthe forward array is:\n\n";
for (int i = 0; i < size; ++i)
    cout << myarr[i];

The while (*end != '\0') thing wouldn't work either. Pass the size as an argument to the function.
I know what you are trying to do, it is just you seem hung up on pedantic detail.

So we already established it works with size 17, and explained why, so why not go with that?

Also, you initialised with a string literal which means it is const char *, so you can't modify it later.

So try using braces with comma separated values:

char myarr[size]= {'a', 'b', 'c', ....}

You could get away with not specifying the size of the array, because your code looks for the '\0'.

char myarr[]= {'a', 'b', 'c', ....}

It is possible to figure out the size later with the sizeof operator. There are 2 parts to that, google it.
Topic archived. No new replies allowed.