✓ Solution: String interview

From the course: Nail Your Java Interview

  • Course details

    Have you lined up an interview for a Java developer position? In this course, review critical Java concepts-and commonly-asked interview questions-to ensure that you approach your Java interviews with confidence. Instructor Kathryn Hodge dives into a discussion of key topics that might appear as questions in an interview, such as the difference between the Java Runtime Environment (JRE) and the Java Development Kit (JDK). She also goes over string manipulation, data structures, and essential object-oriented programming concepts. Throughout the course, Kathryn offers examples that lend a real-world context to the concepts she covers.

    Instructor

    • Click here to view Kathryn Hodge’s instructor page

      Kathryn Hodge

      Kathryn Hodge works as a software developer at a media company.

      In addition to her work as a developer, Kathryn runs a YouTube channel for people wanting to learn how to code. Before graduating college, she worked for three years as a computer science teaching assistant. She is very interested in the intersection of media, entertainment, and technology, and is focused on creating software that makes an impact.

    Skills covered in this course

  • Welcome

    (theatrical music) - [Instructor] The challenge was to create an algorithm that finds the number of vowels and the number of consonants in a given string. There are many ways to solve this problem, but here are two different solutions. First, we set up our main method with the appropriate variables and then we call the function with each variables as input. Let's go ahead and take a look at this first algorithm find number of vowels and consonants. Scrolling up, we'll see it's a public static void function on line nine. Here, we have one string parameter called input and we have two variables, vowelsCount and consonantsCount, that both start off at zero. We also have a variable called vowels that has all the possible vowels in the English language. Then we normalize the string with the .toLowerCase method and then trim everything with the .trim method taking out those extra spaces. Next, we take our normalized input and use the .toCharArray method to convert it into a CharArray. Then we iterate here on line 16 through the CharArray and basically count if something is a vowel or if something is a consonant. If the character is a space, we don't count it as a vowel or a consonant. At the end of the method here on line 24, we print out the number of vowels and the number of consonants. Now, we can also solve this problem using ASCII. ASCII is the numerical representation of a character. For example, the character lowercase a is associated with the number 97. Uppercase M is associated with the number 77. We can use the numbers each character is associated with to find out how many vowels and consonants are in a particular string. Let's take a look at how this works. Here, we have our public static void findNumberOfVowelsAndConsonantsWithAscii method. Just like our other method, here on line 32 we get a string as input. We also start off with the same counters of vowelsCount and a consonantsCount and keep track of our string vowels with the uppercase and lowercase aeiouy. Next, we iterate through the input and get the ASCII value of each character. To do this, we simply cast a character to be an integer and it will return that ASCII value and that's happening on line 37 here. Now, the ASCII value determines whether a letter is a character or not. ASCII values between 65 and 90 inclusive are uppercase letters and ASCII values between 97 and 122 inclusive are lowercase letters. By checking if the ASCII value falls into one of these buckets, we can determine if the character is a letter or not. Scrolling down here on line 41, we check if it's a vowel or if it's a character. If it's not a letter, then nothing happens and we continue to iterate through the rest of the string. If the character is a letter, then we see here on line 41 if it is a vowel by checking the vowel string with the .contains method. If it is not a vowel, then we increment the consonant count. If it is a vowel, then we increment the vowels count. Then at the end of this method on line 50, we go ahead and print out the number of vowels and the number of consonants. Let's go ahead and run the program and then move up this console window and we see there are two vowels in Hello, three consonants in Hello, 10 vowels in there is a quiet Mouse, eight consonants there, and then four vowels and four consonants in I am happy. And this is the same for algo two.

  • Practice while you learn with exercise files

    Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.

    Download the exercise files for this course. Get started with a free trial today.

  • Download courses and learn on the go

    Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.

    Watch this course anytime, anywhere. Get started with a free trial today.

Contents