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
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:
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.
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!!!
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.
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.