char mychar[26];
int i=0;
while(mychar[i]!='\0') // This loop will go through all of the characters in the string until it reaches \0
{
i++;
}
i--; // This will go to the last character of the string
cout<<mychar[i]<<endl; // Display last character
dvader123's way is correct, but takes longer time. Because you are looping with a loop size equals to the string size - 1.
So, it's better to direct access the character at the position you want. Here's how it works.
When you declare a string and it is eventually set, for example, to "Hello", it's actually an array of characters.
An Array is a set of pointers that point to subsequent in-order places in the memory which contains data of the type you declared the array with. In this case, char.
When you use square brackets in arrays [] or at in strings, you actually tell your program to do something like this:
1. Fetch the address of the pointer to the first element in the array.
2. Identify the data type of the elements and get its byte size. In our case char, the byte size is one.
3. Now, we know how many bytes the array allocates and the order of the needed element, so the needed byte which has our data (letter) is d away from the first pointer; where d = (byte size of one character) * position you requested .
So, no looping done, so this operation is O(1) while the looping way is O(n). Look up time complexity analysis for more info.