You and I both know very well which letters are vowels and which are consonants. We need to teach these concepts to intelligent robots, so we'll write a program to train them. The user will enter a string from the keyboard. Then a special method will help to split the entered data into two strings: one with all the vowels and another with only the consonants.
Vowels and consonants
You must be signed in to leave a comment
12 May 2021, 14:25
many different ways to solve this task. found it easiest to create 3 lists; read though the input list to separate vowels from non-vowels. One thing has to be done is to get rid of the empty spaces. you can do that right after the text is read by using s.replaceAll("\\s+", ""); or use if(!Character.isWhitespace(c) in the for-each loop to only add the characters, not the empty spaces. This was a good challenge. Solved with some googling and it was fun. Happy coding! :)
27 March 2021, 13:08
I created two separate strings, one for vowels and another for consonants and loop through the input stringthen i checked if a given char is vowel or not if it is a vowel i concated it to the vowels otherwise to the consonants and added a space at the end then finally and printed both vowels and consonants string using two println statements and used strings trim and replaceAll functions to remove unwanted space characters and added a finaly space at the end as per the requirement of the task.
16 March 2021, 19:59
Took me whole day to figure it out. The problem is that the task requires that user input goes into single string, which then you must parse/convert/access by char/whatever else comes on your mind. Getting vowels is relatively easy, looping through an input string and using if to print out returned trues (which are vowels by isVowel method). Harder thing is to get the rest, ie. consonants. One could simply create another method (for instance isConsonant), use the same logic (loop through a pre-defined array of consonants), but this is out of question because it doesn't comply with the task requirements (because that consonant array must have been static one as well, in order to access it properly). I created an ArrayList, and populated it with returned consonants. Because it was pain in the ass to get the space out of this list, I converted this ArrayList to String (and at the same time found out for a StringBuilder, nice tool). Then when I finally got the consonants in a String, it was easier. I used replaceAll with nice ("\\s","") regex that filtered out all empty spaces in that String. Finally, the String was converted to an Array as the task requirement was to print out the consonants with one space between them, which is easily feasible with looping through an Array and printing it out. Suggested solution is simpler and more efficient, it uses a couple of shortcut tricks. Very useful task indeed.
15 October 2020, 19:41
Didn't filter out the numbers and passed the tests. But I removed all spaces. Replaced them with "" while initializing an array of chars. If you choose to do so, try googling replaceAll().
28 September 2020, 22:38
just a few things I learned while coding this with the help of intellij; 1. You can have an ArrayList of type Character. 2. You can check if a character is an empty space by using single quotes (eg. c == ' ') 3. To iterate a string you can use s.charAt(i)
14 August 2020, 19:03
When looking for consonants filter out the spaces ' ' (use ' ' for characters) to get "S m m ."
6 June 2020, 19:11
That was a kickass exercise. After finally solving the algorithm task, this was a cakewalk. So excited at the results.
17 June 2020, 11:57
Nice and different!
5 December 2019, 01:19
Code is running, but getting error: "The second line should contain only the consonants and punctuation marks from the input string, separated by a space" I added a filter to remove numbers - not specified in the task but ok... It's still not running. Sample output is: abc12abc12.! a a b c b c . ! Process finished with exit code 0 What is going wrong here?
6 December 2019, 07:38
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.
24 January 2019, 02:54
Running in Intellij but failing for server for all cases
24 January 2019, 07:27
Please create a question in the "Help" section and show your code there.