Hi, I am having trouble understanding part of this code below. I explain my problem below the code. Any help greatly appreciated. I have previously posted this post here. other post But still have questions. The problem code snippet is in bold.
I don't understand what this line is for
it looks like it points to the lets say E-A position which would be 4, then ++ that which is 5, but I don't know what it is for? please.
I guess bold doesn't work in code mode.
I also don't understand how this outputs the number of letters for each index
Well, it isn't in bold, because the bold tags don't work inside code tags.
Let's have a look.
Note that upper is an array of ints, declared in line 21. So what is between the square brackets is an array index. current is a char, declared in line 24. In lines 31 - 41, there's a loop that goes over all the characters in line. In line 33, the current character is assigned to current.
The data type char works almost like an integer: it's a number that is the Unicode code for a character. You can do arithmetic with chars, and that's what happens between the square brackets: 'A' is subtracted from current. So, you get an array index where A is 0, B is 1, ... Z is 26.
So, upper[current-'A'] refers to one of the integers in the array. If current is 'A', then it is upper, if current is 'B', then it's upper etc.
Now, at the end you have the ++ postfix increment operator. This just adds 1 to whatever the operator is applied to and stores the result. Note that something++ means the same as something = something + 1. So, the line upper[current-'A']++ means upper[current-'A'] = upper[current-'A'] + 1.
So, what it does is count the letters in the sentence. Each time it finds an 'A', upper is incremented, each time it finds a 'B', upper is incremented etc. There's another array for the lower-case letters and non-letters are all counted with a single counter.
Ok, other than this 5 page code example previously in my book which I just ignored rather than messing with it, this was the hardest code to understand in my book, and it is page 360 only out of 715 pages. LOL. After stretching my brain , and painfully forging new pathways, I have included my comments to explain the code which "I think" are correct, if anyone was curious. Thank you again!
Here it is!
I hope this is correct, I don't want to confuse anyone. thanks. Derek
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
a bit of art, as a gift, that will fit in a stocking