20 Replies - 708 Views - Last Post: 23 February 2013 - 10:44 AM
#1
Alternating vowels and consonants
Posted 13 February 2013 - 02:04 PM
Replies To: Alternating vowels and consonants
#2
Re: Alternating vowels and consonants
Posted 13 February 2013 - 02:17 PM
#3
Re: Alternating vowels and consonants
Posted 13 February 2013 - 06:13 PM
for(int z=0;z<len;z++){
charNum = s.charAt(z);
if(charNum=='A'||charNum=='E'||charNum=='I'||charNum=='O'||charNum=='U')vow++;
}
You'll need a similar code to this
#4
Re: Alternating vowels and consonants
Posted 17 February 2013 - 12:01 PM
#5
Re: Alternating vowels and consonants
Posted 17 February 2013 - 12:03 PM
#6
Re: Alternating vowels and consonants
Posted 17 February 2013 - 01:36 PM
g00se, on 17 February 2013 - 12:03 PM, said:
this is all i have so far i want to use the for loop to run the checking
import javax.swing.JOptionPane;
public class AlternatingVowelsAndConsonants
{
public static void main(String [] args)
{
String results;
String vowels = "aeiou";
String consonants = "bcdfghjklmnpqrstvwxyz";
String userInput;
userInput = JOptionPane.showInputDialog(null, "Enter a word ");
String strippedInput = userInput.replaceAll("\\W", "");
char[] charArray = strippedInput.toCharArray();
if (userInput.equals(""))
results = "Input Required";
else
{
for ( int index = 0; index < charArray.length; index++)
}
JOptionPane.showMessageDialog(null,results);
}
}
#7
Re: Alternating vowels and consonants
Posted 17 February 2013 - 02:24 PM
make an array of boolean the size of your String
boolean[] b = new boolean[str.length()];
that will give you an array of 5 boolean initialized to false
set to true all the slot used by voyels. You will end up with
true false true false true
loop from 1 to length if the previous entry == the actual entry you failed
int i;
for(i = 1; i < b.length; ++i) {
if(b[1] == b[i-1]}
break;
}
if(i == b.length)
... they alternate
else
... they don't
#8
Re: Alternating vowels and consonants
Posted 17 February 2013 - 02:27 PM
#9
Re: Alternating vowels and consonants
Posted 17 February 2013 - 02:46 PM
That logic will work with every word
#10
Re: Alternating vowels and consonants
Posted 17 February 2013 - 03:07 PM
pbl, on 17 February 2013 - 02:46 PM, said:
That logic will work with every word
my coding just got very messy with the help of a friend im so confused
import javax.swing.JOptionPane;
public class AlternatingVowelsAndConsonants
{
public static void main(String [] args)
{
String results;
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'};
String userInput;
userInput = JOptionPane.showInputDialog(null, "Enter a word ");
String strippedInput = userInput.replaceAll("\\W", "");
char[] charArray = strippedInput.toCharArray();
// comparing method
if (userInput.equals(""))
results = "Input Required";
else
for ( int index = 0; index < charArray.length; index++)
{
char letter = charArray[index];
if (index / 2 == 0)
{
for (int i = 0; i < consonants.length; i++)
{
if(letter == consonants[i])
{
word = true;
}
}
}
else
{
for (int i = 0; i < vowels.length; i++)
{
if(letter == vowels[i])
{
word = true;
}
}
if (word = false)
{
break;
}
}
}
JOptionPane.showMessageDialog(null,result);
// in result it should say yes it is a valid word or no, i have to add that in there somewhere but its so messy and isnt running
}
}
#11
Re: Alternating vowels and consonants
Posted 17 February 2013 - 03:15 PM
String str = ... get input from user
.. remove space and punctuation
boolean[] b = new boolean[str.length()];
for(int i = 0; i < str.length(); ++i) {
switch(str.charAt(i)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y': // yes in French :)/>
b[i] = true;
}
}
#12
Re: Alternating vowels and consonants
Posted 17 February 2013 - 03:37 PM
pbl, on 17 February 2013 - 03:15 PM, said:
String str = ... get input from user
.. remove space and punctuation
boolean[] b = new boolean[str.length()];
for(int i = 0; i < str.length(); ++i) {
switch(str.charAt(i)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y': // yes in French :)/>/>
b[i] = true;
}
}
so i dont have to put the string into a char array?
#13
Re: Alternating vowels and consonants
Posted 17 February 2013 - 04:34 PM
ctroop4ever, on 18 February 2013 - 12:37 AM, said:
No, you don't. Actually you don't need the boolean array either. You could use a boolean variable that you initialize it based on whether the first character is a vowel or not. Then, in the for loop, you initialize that variable to its opposite so that it can alternate with each iteration(let's say you have boolean b; you switch it using b = !b;) ... then you compare inside the for loop with hasVowel... if false, then use break to get out of the loop... this way you won't have to iterate through the entire string...
This post has been edited by ccdan: 17 February 2013 - 04:47 PM
#14
Re: Alternating vowels and consonants
Posted 17 February 2013 - 04:56 PM
/** Checks whether a String has alternating vowels and consonants */
static boolean alternatingVowelConsonant(String str){
boolean b = isVowel(str.charAt(0));
for (int i = 1; i < str.length(); i++){
b = !b;
if (b != isVowel(str.charAt(i))) return false;
}
return true;
}
/** Checks whether a char is a vowel or not */
static boolean isVowel(char ch) {
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':return true;
}
return false;
}
This post has been edited by ccdan: 17 February 2013 - 04:57 PM
#15
Re: Alternating vowels and consonants
Posted 17 February 2013 - 05:05 PM
This post has been edited by burakaltr: 17 February 2013 - 10:58 PM
|
|

New Topic/Question
Reply



MultiQuote




|