diff --git a/README.md b/README.md index a670384e..fbb43c48 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines. 1. [Day 1 -- The Fizz Buzz Problem](./Day1/) -- [http://codetoexpress.tech/dc/day1](http://codetoexpress.tech/dc/day1) 2. [Day 2 -- String Reversal + Palindrome](./Day2/) -- [http://codetoexpress.tech/dc/day2/](http://codetoexpress.tech/dc/day2) 3. [Day 3 -- The Hamming Distance Problem](./Day3/) -- [http://codetoexpress.tech/dc/day3/](http://codetoexpress.tech/dc/day3/) +4. [Day 4 -- Num Vowels + Max Chars](./day4/) -- [http://codetoexpress.tech/dc/day4/](http://codetoexpress.tech/dc/day4/) ## Timeline diff --git a/day4/Java/NumVowels.java b/day4/Java/NumVowels.java new file mode 100644 index 00000000..964e573c --- /dev/null +++ b/day4/Java/NumVowels.java @@ -0,0 +1,33 @@ +import java.util.Scanner; + +public class NumVowels { + public static void main (String[] args) { + // set count = 0 + int count = 0; + + // Input the string + Scanner input = new Scanner(System.in); + System.out.println("/* ===== Number of Vowels ===== */"); + System.out.print("\nEnter the string: "); + String str = input.next(); + + // Convert input string to lower case + str = str.toLowerCase(); + + // Run a loop from 0 to string length + for (int i=0; i= 0) { + count++; + } + } + + // Print and return the number of vowels + console.log(`The number of vowels in ${str} is = ${count}`); + return count; +} + +numVowels ('hEllo'); +numVowels ('Greetings'); +``` + +## [Solution 3](./JavaScript/partA_sol3.js) + +```js +// Store vowels in an array +// check each character of the string with each element in vowels array + +function numVowels (str) { + // Initialize count to be zero + let count = 0; + + // Define an array of vowels + let vowels = ['a', 'e', 'i', 'o', 'u']; + + // Check each character of string + for (let char of str) { + for (let vowel of vowels) { + if (char.toLowerCase() === vowel) count++; + } + } + + // Print the result + console.log(`The number of vowels in "${str}" is = ${count}`); + return count; +} + +numVowels ('hello'); +numVowels ('Greetings'); +``` + +## [Solution 4](./JavaScript/partA_sol4.js) + +```js +// Similar to previous method +// Instead of storing vowels in an array, store them in an object as keys and values as the number of occurances + +function numVowels (str) { + console.log('\nString: ', str); + + // Initialize count to be zero + let count = 0; + + // Store the vowels in an object + let vowels = { + 'a': 0, + 'b': 0, + 'c': 0, + 'd': 0, + 'e': 0 + }; + + // Iterate through each character of string to check vowels + for (let char of str) { + for (vowel in vowels) { + if (char.toLowerCase() === vowel) { + count++; + vowels[vowel]++; + } + } + } + + // Print the result + console.log('Vowel Count: '); + for (let vowel in vowels) { + console.log(`Vowel: ${vowel} appears ${vowels[vowel]} number of times in the string "${str}"`); + } + + console.log("Total number of vowels: " + count); + return count; +} + +numVowels ('hello'); +numVowels ('Greetings'); +``` + +## Java Implementatio + +### [Solution 1](./Java/NumVowels.java) + +```java +import java.util.Scanner; + +public class NumVowels { + public static void main (String[] args) { + // set count = 0 + int count = 0; + + // Input the string + Scanner input = new Scanner(System.in); + System.out.println("/* ===== Number of Vowels ===== */"); + System.out.print("\nEnter the string: "); + String str = input.next(); + + // Convert input string to lower case + str = str.toLowerCase(); + + // Run a loop from 0 to string length + for (int i=0; i