• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Letter counter program. having trouble understanding it.

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

System.out.println (": " + lower[letter]);
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[0], if current is 'B', then it's upper[1] 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[0] is incremented, each time it finds a 'B', upper[1] is incremented etc. There's another array for the lower-case letters and non-letters are all counted with a single counter.
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jasper, I am checking out the code again now.
 
derek smythe
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic