Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a97e50
commit 05e36a6
Showing
11 changed files
with
531 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<str.length(); i++) { | ||
if ( | ||
str.charAt(i) == 'a' || | ||
str.charAt(i) == 'e' || | ||
str.charAt(i) == 'i' || | ||
str.charAt(i) == 'o' || | ||
str.charAt(i) == 'u' | ||
) { | ||
count++; | ||
} | ||
} | ||
|
||
// Print the result | ||
System.out.println("Number of vowels in \"" + str + "\" = " + count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import java.util.Scanner; | ||
|
||
public class NumVowels2 { | ||
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(); | ||
|
||
// Create an array of vowels | ||
char[] vowels = new char[]{ 'a', 'e', 'i', 'o', 'u' }; | ||
|
||
// Run a loop from 0 to string length | ||
for (int i=0; i<str.length(); i++) { | ||
// Check whether the current character exists in the vowels array | ||
for (int j=0; j<5; j++) { | ||
if (str.charAt(i) == vowels[j]) { | ||
count++; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// Print the result | ||
System.out.println("Number of vowels in \"" + str + "\" = " + count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Step 1: Set count = 0 | ||
// Step 2: Run a loop from i=0 to i=string_length | ||
// Step 3: In each iteration check whether the character at position i is ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’. If the condition passes, increment the value of count. | ||
// Step 4: Print and return the results. | ||
|
||
function numVowels (str) { | ||
let count = 0; | ||
|
||
for (let i=0; i<str.length; i++) { | ||
if ( | ||
str[i].toLowerCase() === 'a' || | ||
str[i].toLowerCase() === 'e' || | ||
str[i].toLowerCase() === 'i' || | ||
str[i].toLowerCase() === 'o' || | ||
str[i].toLowerCase() === 'u' | ||
) { | ||
count++; | ||
} | ||
} | ||
|
||
console.log (`The number of vowels in ${str} is = ${count}`); | ||
} | ||
|
||
numVowels ('hello'); | ||
numVowels ('Greetings'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Step 1: Set count = 0 | ||
// Step 2: Store the vowels in an array (say, vowels) | ||
// Step 3: Run a loop from i=0 to i=string_length | ||
// Step 4: In each iteration check whether the current character (at position i) of the string is there in the vowels array using indexOf() method | ||
// Step 5: Increment the count if the above condition is satisfied. | ||
// Step 6: Print the result | ||
|
||
function numVowels (str) { | ||
let count = 0; | ||
let vowels = ['a', 'e', 'i', 'o', 'u']; | ||
|
||
// Iterate over each character in the string | ||
for (let i=0; i<str.length; i++) { | ||
if (vowels.indexOf(str[i].toLowerCase()) > 0) { | ||
count++; | ||
} | ||
} | ||
|
||
console.log (`The number of vowels in ${str} is = ${count}`); | ||
} | ||
|
||
numVowels ('hello'); | ||
numVowels ('Greetings'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Step 1: Set count = 0 | ||
// Step 2: Store the vowels in an array (say, vowels) | ||
// Step 3: Run a loop from i=0 to i=string_length | ||
// Step 4: In each iteration, run a nested loop from j=0 to j<5 (5 vowels), and check whether string[i] exists in vowels[j]. | ||
// Step 5: Increment the count if the above condition is satisfied. | ||
// Step 6: Print the result | ||
|
||
function numVowels (str) { | ||
let count = 0; | ||
let vowels = ['a', 'e', 'i', 'o', 'u']; | ||
|
||
for (let char of str) { | ||
for (let vowel of vowels) { | ||
if (char.toLowerCase() === vowel) count++; | ||
} | ||
} | ||
|
||
console.log (`The number of vowels in ${str} is = ${count}`); | ||
} | ||
|
||
numVowels ('hello'); | ||
numVowels ('Greetings'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Step 1: Set count = 0 | ||
// Step 2: Store the vowels in an object (vowels) with initial values as zero | ||
// Step 3: Iterate through each character of string and check whether it exists in the vowels object | ||
// Step 4: If it exists, increment its value, and the value of count variable | ||
// Step 5: Print the result | ||
|
||
function numVowels (str) { | ||
let count = 0; | ||
let vowels = { | ||
'a': 0, | ||
'e': 0, | ||
'i': 0, | ||
'o': 0, | ||
'u': 0 | ||
}; | ||
|
||
for (let char of str) { | ||
// if (vowels[char]) { | ||
// vowels [char]++; | ||
// count++; | ||
// } | ||
|
||
for (vowel in vowels) { | ||
if (char.toLowerCase() === vowel) { | ||
count++; | ||
vowels[vowel]++ | ||
} | ||
} | ||
} | ||
|
||
console.log (`The number of vowels in ${str} is = ${count}`); | ||
|
||
} | ||
|
||
numVowels ('hello'); | ||
numVowels ('Greetings'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Step 1: Declare an empty object letterCount which will store characters as keys and their frequencies as values. | ||
// Step 2: Iterate through each character of input string and add it to the object if it doesn't already exist, else increment its’s value | ||
// Step 3: Find the key with maximum value in the letterCount object. | ||
|
||
function maxChars (str) { | ||
let letterCount = {}; | ||
|
||
// Complete the letter count object | ||
for(let currentChar of str) { | ||
let flag = 0; | ||
for (letter in letterCount) { | ||
if (currentChar.toLowerCase() === letter) { | ||
flag = 1; | ||
letterCount[letter]++; | ||
} | ||
} | ||
|
||
if (flag === 0) { | ||
letterCount[currentChar] = 1; | ||
} | ||
} | ||
|
||
// Find the maximum value key from the letterCount | ||
let max = 0, maxChar = ''; | ||
for (letter in letterCount) { | ||
if (letterCount[letter] >= max) { | ||
max = letterCount[letter]; | ||
maxChar = letter; | ||
} | ||
} | ||
|
||
// Print the result | ||
console.log(`The maximum frequency letter is: "${maxChar}" and it appears ${letterCount[maxChar]} number of times!`); | ||
return letterCount[maxChar]; | ||
} | ||
|
||
maxChars('helllllo worlld'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Answer similar to first solution, but Step 2 has been replaced by a one liner | ||
|
||
function maxChars (sentence) { | ||
let characters = {}; | ||
|
||
// Update characters object with count of each character in sentence | ||
for (let letter of sentence) { | ||
// increment the count of existing letter OR add the new character with value = 1 | ||
characters[letter] = characters[letter] + 1 || 1; | ||
} | ||
|
||
let max = 0, | ||
maxChar = ''; | ||
|
||
for (let character in characters) { | ||
if (max < characters[character]) { | ||
max = characters[character]; | ||
maxChar = character; | ||
} | ||
} | ||
|
||
// Print the result | ||
console.log(`The maximum frequency letter is: "${maxChar}" and it appears ${characters[maxChar]} number of times!`); | ||
return characters[maxChar]; | ||
} | ||
|
||
maxChars('helllllo worlld'); |
Oops, something went wrong.