Let's look at an example. The array has room for 20 characters. They are numbered from 0 to 19.
When the user types the word "madam", it occupies the first six of those character positions.
0 1 2 3 4 5 6 17 18 19
_________________________________________________________________________________
| m | a | d | a | m |\0 | | | | | | | | | | | | | | |
_________________________________________________________________________________
|
We need to distinguish between the subscripts (the numbers 0 to 19) and the contents of the array (the letters "madam" and some other unknown characters).
The variables a and b are used as subscripts (or indexes). These allow the string to be examined step-by-step, one position at a time. If instead we went direct to the actual letters, there would be no way to proceed to look at the next or preceding character.
Think of the subscripts a and b as pointing (though pointers is another topic) at the current pair of characters.
char a = word[0];
would place the letter 'm' (from the word "madam") into a. But a+1 would give the letter 'n' (the next letter of the alphabet), a+2 would give the letter 'o' and so on.
int a = 0;
allows us to access the first letter of the word. And after that, a+1 will be used to access the next letter of the word.
When assigning the value of b, again it is the subscript. But it cannot be 20 because word[20] is invalid, it is outside the array). Nor do we use b = 19, because the user typed "madam" and it only has five letters. So we use strlen(word) which gives the length 5, but the last letter of the word is position 4 because array numbering starts from zero, not one.
At line 5,
if (word[a] == word[b])
there is a
mistake, it should test for "not equal",
!=
, and set the status to false if the pair of characters are indeed different.
I hope that explained some of it. I'm not sure I covered it all. It's sometimes easier to talk than it is to write.
By the way, you might find it useful to go through the tutorial pages on character arrays, it is much more thorough and should make things clearer:
http://www.cplusplus.com/doc/tutorial/ntcs/
and arrays in general
http://www.cplusplus.com/doc/tutorial/arrays/