The \0 character that supposed to be included is overwritten and the array doesn't know when the end comes. Also when I manually insert a null character I lose the shifted char array. SO with this being said I have some questions.
You are confused. The null (\0) character doesn't get put there magically by the compiler. If you want to be able to treat that char array as a string, YOU have to allow space for the null character, and YOU have to put it there. If you want to be able to print it just by writing
cout << letters << endl; , you should do this:
char letters[27];
int i;
for(i = 0; i < 26; i++) {
letters[i] = 'A' + i;
}
letters[i] = '\0'; // here, i==26
cout << letters << endl;
1.) When memory is allocated for identifiers, do the identifiers declared in the same block share the same space?
2.) How do I get the compiler to allocate identifiers in separate memory spaces?
You are confused here as well. Your professor probably said something to the effect that the identifiers declared within a particular block of code are only accessible while that block of code is executing. Even your example shows that they don't share the same space -- if they did, the second array would have completely overwritten the first one. Instead, as your example shows, the second array follows immediately after the first one -- as you would logically expect. So you don't have to do anything special -- each identifier gets its own memory space automatically. But you have to be sure that you are allocating ENOUGH space.
As to the "solution" you posted in Post #9, it is incorrect. You declared each of the arrays to be 26 chars, so, for example, the upperCase array runs from upperCase[0]-upperCase[25]. If you write a '\0' to upperCase[26] you are writing in memory that doesn't belong to that array. You are writing it in memory space that belongs to the NEXT variable that you declared. So if the next variable is the lowerCase array, then the space that you are calling upperCase[26] is REALLY lowerCase[0]; when you assign letters to lowerCase, the 'a' will overwrite the '\0' and you have the same problem as before. Or if you switch things around and declare
int shift; between the two char arrays, then either the value assigned to shift will overwrite the '\0' or the '\0' will overwrite shift, depending on which you do last. Instead, the solution is simply to allocate 27 bytes to each array instead of 26, using the last byte of each array to hold the '\0', as in my example above.