I am trying to write a code that reads a text file of many words ( I got this part). Then I need to read each word and find/print those words that have all five vowels in it (in consecutive order: a,e,i,o,u). I am very confused how I would approach doing this!! please help!!!
15 Replies - 6319 Views - Last Post: 15 August 2009 - 12:51 PM
#1
Writing a code to find words with all five consecutive vowels
Posted 28 October 2008 - 12:31 PM
Replies To: Writing a code to find words with all five consecutive vowels
#2
Re: Writing a code to find words with all five consecutive vowels
Posted 28 October 2008 - 12:46 PM
Simply
would do it
word.matches(".*a.*e.*i.*o.*u.*");
would do it
This post has been edited by g00se: 28 October 2008 - 12:46 PM
#3
Re: Writing a code to find words with all five consecutive vowels
Posted 28 October 2008 - 12:48 PM
Is there another way to do this because I have not learned that in my class yet. For instance using a loop of some sort?
#4
Re: Writing a code to find words with all five consecutive vowels
Posted 28 October 2008 - 01:05 PM
you can split the input String into an array of Strings...then traverse through the array and check every element in the arrray
this will split the input into an array of Strings..
String [] x = input.split( " " );
this will split the input into an array of Strings..
#5
Re: Writing a code to find words with all five consecutive vowels
Posted 28 October 2008 - 01:09 PM
Ok that makes sense to me. How can I transverse through the array and check for each vowel then to make sure the vowels are in order?
#6
Re: Writing a code to find words with all five consecutive vowels
Posted 28 October 2008 - 01:20 PM
You could split the String into a character array:
char[] x = input.toCharArray();
And then loop through the char array and check for the occurence of each vowel, taking into account order. So first check for a, when that is found set a flag saying a was found, then search for b, and so on
Then after the loop you can check if all the flags are true
char[] x = input.toCharArray();
And then loop through the char array and check for the occurence of each vowel, taking into account order. So first check for a, when that is found set a flag saying a was found, then search for b, and so on
for(int i = 0; i < x.length; i++){
if(!aFound){
if(x[i] == 'a') aFound == true;
} else if(!bFound){
if(x[i] == 'b') bFound == true;
//And so on
Then after the loop you can check if all the flags are true
#7
Re: Writing a code to find words with all five consecutive vowels
Posted 28 October 2008 - 01:27 PM
Thank you that was very helpful.
I am curious as to what the statement:
else if(!bFound){
does?
Also how would I go about asking to print the flagged ones with all the vowels?
I am curious as to what the statement:
else if(!bFound){
does?
Also how would I go about asking to print the flagged ones with all the vowels?
#8
Re: Writing a code to find words with all five consecutive vowels
Posted 28 October 2008 - 01:34 PM
the statement:
else if(!bFound){
says that is b is not(!) yet found execute this code, bFound is a boolean variable to keep tract of whether or not the b vowel was yet found
To print the flagged ones you would have to check if all the flags were true after you went through the above loops.
So for a list of words you would loop through the list and each time you would loop through the word you are currently at whith the above loop, and after that loop you would check if all the flags were true, if they were you would print the word, if not you would move on to the next word.
else if(!bFound){
says that is b is not(!) yet found execute this code, bFound is a boolean variable to keep tract of whether or not the b vowel was yet found
To print the flagged ones you would have to check if all the flags were true after you went through the above loops.
So for a list of words you would loop through the list and each time you would loop through the word you are currently at whith the above loop, and after that loop you would check if all the flags were true, if they were you would print the word, if not you would move on to the next word.
#9
Re: Writing a code to find words with all five consecutive vowels
Posted 28 October 2008 - 01:45 PM
I just want to make sure I am understanding this correctly so I wrote out a mock psuedo code, please tell me if I am correct.
READ the file to get the words
DECLARE each word into a string of characters
Split the String into a character array
FOR loop to read through the character array
Check the occurrence of each vowel
Start IF ‘a’ is found
IF character equals ‘a’ then mark as TRUE (found)
ELSE IF a is not found keep executing this code
Then search ‘e’: Start IF ‘e’ is found
IF character equals ‘e’ then mark as TRUE (found)
ELSE IF a is not found keep executing this code
Continue through each vowel remaining vowel (i,o,u)
CHECK if word has all five TRUE(found)
IF so, PRINT word
ELSE, move to next word and check flags.
READ the file to get the words
DECLARE each word into a string of characters
Split the String into a character array
FOR loop to read through the character array
Check the occurrence of each vowel
Start IF ‘a’ is found
IF character equals ‘a’ then mark as TRUE (found)
ELSE IF a is not found keep executing this code
Then search ‘e’: Start IF ‘e’ is found
IF character equals ‘e’ then mark as TRUE (found)
ELSE IF a is not found keep executing this code
Continue through each vowel remaining vowel (i,o,u)
CHECK if word has all five TRUE(found)
IF so, PRINT word
ELSE, move to next word and check flags.
#10
Re: Writing a code to find words with all five consecutive vowels
Posted 28 October 2008 - 06:12 PM
The for and if are indented to show the nesting. also for ...i,o,u you would repeat the two if's for each of these
READ the file to get the words
STORE each word in a String of words
For loop to read each word in the array of words
--Split the String into a character array
--FOR loop to read through the character array
----Check the occurrence of each vowel
------Start IF ‘a’ is not yet found
--------IF character equals ‘a’ then mark as TRUE (found)
--------ELSE IF a is not found move on to next character
------ELSE IF 'e' is not yet found
--------IF character equals ‘e’ then mark as TRUE (found)
--------ELSE IF e is not found move on to next character
...i
...o
...u
--CHECK if word has all five TRUE(found)
--IF so, PRINT word
--ELSE, move to next word
READ the file to get the words
STORE each word in a String of words
For loop to read each word in the array of words
--Split the String into a character array
--FOR loop to read through the character array
----Check the occurrence of each vowel
------Start IF ‘a’ is not yet found
--------IF character equals ‘a’ then mark as TRUE (found)
--------ELSE IF a is not found move on to next character
------ELSE IF 'e' is not yet found
--------IF character equals ‘e’ then mark as TRUE (found)
--------ELSE IF e is not found move on to next character
...i
...o
...u
--CHECK if word has all five TRUE(found)
--IF so, PRINT word
--ELSE, move to next word
#11
Re: Writing a code to find words with all five consecutive vowels
Posted 28 October 2008 - 06:43 PM
Thanks so much BigAnt I really appreciate your help!
#12
Re: Writing a code to find words with all five consecutive vowels
Posted 14 August 2009 - 09:41 AM
[quote name='BigAnt' date='28 Oct, 2008 - 05:12 PM' post='448790']
can you please tell me how to check vowels without putting them in an array , using a user-defined function (such as isVowel)...???
i mean just by writing a single line code....
can you please tell me how to check vowels without putting them in an array , using a user-defined function (such as isVowel)...???
i mean just by writing a single line code....
#13
Re: Writing a code to find words with all five consecutive vowels
Posted 14 August 2009 - 09:48 AM
Please don't necro threads. You can do a switch stame.
switch(word.equalsIgnoreCase) {
a:...
e:...
i:...
o:...
u:...
}
switch(word.equalsIgnoreCase) {
a:...
e:...
i:...
o:...
u:...
}
#14
Re: Writing a code to find words with all five consecutive vowels
Posted 14 August 2009 - 12:24 PM
Let's use an int counter field instead of a million booleans. Let me demonstrate:
Remember, a String is a class with an array in the background. Hope this helps!
String x = "abcde";
for(int a = 0, y = 0; a < x.length; a++){
if(y == 0 &&
(x.charAt(a) == 'a' || x.charAt(a) == 'A')) y++;
if(y == 1 &&
(x.charAt(a) == 'e' || x.charAt(a) == 'E')) y++;
//do the same thing for y == 2, 3, 4 w/i, o & u respectively
}
Remember, a String is a class with an array in the background. Hope this helps!
#15
Re: Writing a code to find words with all five consecutive vowels
Posted 15 August 2009 - 12:31 PM
Sorry, but you'll need to use multiple lines of code to solve this problem. I applaud your desire for efficiency though.
|
|

New Topic/Question
Reply




MultiQuote









|