My own 30 days of coding challenge!
So today, I will start my own personal 30 day challenge and hopefully at the end of this challenge I will learn even more and get even better in programming.
For 30 days, I will create a piece of code everyday using Java programming language and NetBeans IDE for a total of 30 programs within this challenge (or more depending on my mood). I will explain what codes and stuff I've wrote and I will also share the codes with you and I will also upload the source project.
I am doing this challenge to learn even more in the world of Java language, because it's been a year since I code using Java so I'm back to square zero(and I only know the basic of Java so this might start confusing at first because I forgot some syntax of it) and as what The great Roman leader Julius Caesar said,
Experience is the teacher of all things
and the good thing about it is that you don't have to pay this teacher of yours, absolutely free. I can't promise to post everyday since this month is so very busy for a graduating student like me but I will do my best to post everyday until the last day.
So without further ado, let's get started with the first day right now!
Day 1: Segregating consonants to vowels
I've decided that for the first week of this challenge I'll do something easy, so I came up with disemvoweler!
Name: Disemvoweler
Description: This program removes every vowels and whitespace found in a string. It should output the resulting disemvoweled string with the removed vowels concatenated to the end of it.
Example:
Input: Hello world
Output: hllwrld eoo
So what I need to do for this to work is, to set the user-inputted string into the variable and have a for-loop for every letter of the strings(this excludes whitespaces and any other special characters) and check if it's a vowel or a consonant. Then after determining the letter's speech sounds, concatenate that letter into its corresponding consonant or vowel group variable. And Lastly, after the for-loop, print out the consonant and vowel group variables.
Just so you know,
vowels and consonants are sounds, not letters. Depending on your accent and how thinly you slice them, there are about 20 vowels and 24 consonants.
Source: Vowels and Consonants are sounds, not letters | Spelfabet
But let's stick to what we've used to know when we're on kindergarten, five vowels and the remaining are consonants. So yeah back to the topic!
There are two methods that I came up with, one is the manual or the longer version and the second one is the easier version
General Variables:
Scanner input;
String inputString;
String groupOfVowels;
String groupOfConsonants;
char letter;
Variables for method 1:
char[] vowels;
char[] consonants;
Variable for method 2:
int posOfLetter;
Method 1
In this method we will manually checking whether the current selected letter in the given string is vowel or consonant using for-loop for every letter and another for-loop inside it for the checking and saving it into its corresponding variable.
Scanner input = new Scanner(System.in);
String inputString;
String groupOfVowels = "";
String groupOfConsonants = "";
char letter;
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
char[] consonants = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'};
We first initialized the variables that we will going to use. Please do note that we need to import the scanner first with import java.util.Scanner
System.out.println("Enter a string:");
inputString = input.nextLine();
inputString = inputString.toLowerCase();
This will let the user enter a string and save it in an inputString
variable.
// Get every character in inputString and proceed
// with another for-loop inside for both vowels and consonants.
for (int i = 0; i < inputString.length(); i++) {
// Get the character positioned at ith in the inputString.
letter = inputString.charAt(i);
// Check if letter is a vowel
for (int j = 0; j < vowels.length; j++) {
if (letter == vowels[j]) {
// If true then concatenate the letter in the groupOfVowels variable.
groupOfVowels += vowels[j];
break;
}
}
// Check if letter is a consonant
for (int k = 0; k < consonants.length; k++) {
if (letter == consonants[k]) {
// If true then concatenate the letter in the groupOfConsonants variable.
groupOfConsonants += consonants[k];
break;
}
}
}
The for-loop
and if-condition
are used to check and compare the letter with each item in array list of vowels/consonants variables.
System.out.println("Result: " + groupOfConsonants + " " + groupOfVowels);
And finally, print out the disemvoweled string.
Method 2
In this method, we will going to use the indexOf()
method of Java. In this way we won't be needing the char variable for vowels and consonants and the for-loop
.
Scanner input = new Scanner(System.in);
String inputString;
String groupOfVowels = "";
String groupOfConsonants = "";
char letter;
int posOfLetter;
Same as before but the different is we replaced the char[] vowels
and char[] consonants
with int posOfLetter
.
for (int l = 0; l < inputString.length(); l++) {
letter = inputString.charAt(l);
// Get the position of compared letter in the string of "aeiou"
posOfLetter = "aeiou".indexOf(letter);
// Check if an index is found, if none, posOfLetter is set to -1
if(posOfLetter >= 0)
groupOfVowels += "aeiou".charAt(posOfLetter);
else {
posOfLetter = "bcdfghjklmnpqrstvwxyz".indexOf(letter);
if (posOfLetter >= 0)
groupOfConsonants += "bcdfghjklmnpqrstvwxyz".charAt(posOfLetter);
}
}
System.out.println("Result: " + groupOfConsonants + " " + groupOfVowels);
As you can see, with the use of indexOf()
we make our codes shorter and we only used one for-loop
.
The Result
Enter a string:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Result: lrmpsmdlrstmtcnscttrdpscnglt oeiuooiaeoeeuaiiiei
Note: In the result, only alphabets were shown and converted into lowercase. If you want to increase the difficulty, you can add another conditions for including special characters and concatenating it after the vowels.
And that's it for the first day of the challenge! If you've come this far then a big thank you for you! Let me know in the comment of what should I do next and share your own version of disemvoweler if you can. Please upvote and resteem if you enjoy reading my post thank you.
Download link for this project: Practice 1 - Disemvoweler.rar - 16 KB
Congratulations @jkazuto! You have received a personal award!
Click on the badge to view your Board of Honor.
Do not miss the last post from @steemitboard:
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit