YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← String Traversal - Intro to Java Programming

Get Embed Code
5 Languages

Showing Revision 7 created 06/27/2017 by Dejan Trajkovic.

  1. The for loop is really useful to take words apart into the individual
  2. characters. For example, when we have a word like this, we might want to look
  3. at one character at a time. And what we're going to do in our sample program,
  4. is count how many vowels this word has. Now, why does anyone care how many
  5. vowels a word has? When you know how many vowels there are that gives you an
  6. issue, an idea of how complicated the word is. And sometimes you actually have
  7. to adjust your writing to be simpler. And then it's good to know that so just.
  8. So, how do we do such a a thing. We need to, find, out, each of the characters.
  9. And then, look at it more closely. Over here, you see how to look at the i'th
  10. character. You take the substring, that goes from position i. Up to, but not
  11. including position i plus 1. And that is a string containing a single
  12. character. And we'll let I vary from 0 to the last valid index. That would be
  13. the length minus 1. Notice that I starts at 0. It is less than the length that
  14. makes means it goes up to the length minus 1 and it gets incremented by 1 every
  15. time. This loop that you see here with this fore header and extracting the i-th
  16. letter that's what you use every time that you want to break a word into its
  17. individual characters. Now, onto our specific problem we want to count vowels.
  18. The condition that you see here checks whether the letter is a vowel. it looks
  19. a little backwards. We list all of the vowels, and we ask whether the letter
  20. is any one of those. If we have a vowel. We increment a counter. The rest of
  21. the program is simple. We print the result and also we need to declare a few
  22. variables. Let's run the program. We're supposed to type in a word and we learn
  23. that mouse has three vowels which doesn't surprise us. Actually is the bonus
  24. fact of the day. Some words have more vowels than you think. Here's the French
  25. word for bird, oiseau, and it has five vowels out of six letters. Which is hard
  26. to imitate in English.