Alternating vowels and consonants

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 3135 Views - Last Post: 23 February 2013 - 10:44 AM Rate Topic: -----

#1 ctroop4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 13

Alternating vowels and consonants

Posted 13 February 2013 - 02:04 PM

I am trying to create a program that checks if a sentence composes of alternating vowels and consonants, I have taken out punctuation and put the sentence into a character array, I just can't figure out how to set a for loop that will check if its alternating vowels and consonants.
Is This A Good Question/Topic? 0
  • +

Replies To: Alternating vowels and consonants

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,991
  • Joined: 20-September 08

Re: Alternating vowels and consonants

Posted 13 February 2013 - 02:17 PM

Post code please
Was This Post Helpful? 0
  • +
  • -

#3 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 92
  • View blog
  • Posts: 276
  • Joined: 07-November 10

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
Was This Post Helpful? 0
  • +
  • -

#4 ctroop4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 13

Re: Alternating vowels and consonants

Posted 17 February 2013 - 12:01 PM

It has to check if a word for example is like arara, vowel then consonant and so on
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,991
  • Joined: 20-September 08

Re: Alternating vowels and consonants

Posted 17 February 2013 - 12:03 PM

Post code please
Was This Post Helpful? 0
  • +
  • -

#6 ctroop4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 13

Re: Alternating vowels and consonants

Posted 17 February 2013 - 01:36 PM

View Postg00se, on 17 February 2013 - 12:03 PM, said:

Post code please



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);
		
				
	}
}
		
		
		
		
		

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Alternating vowels and consonants

Posted 17 February 2013 - 02:24 PM

arara ?

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


Was This Post Helpful? 3
  • +
  • -

#8 ctroop4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 13

Re: Alternating vowels and consonants

Posted 17 February 2013 - 02:27 PM

arara was just an example :P like rehabilitative is a word that composes of alternating vowels and consonants
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Alternating vowels and consonants

Posted 17 February 2013 - 02:46 PM

Yes I used your example to show up the logic.
That logic will work with every word
Was This Post Helpful? 0
  • +
  • -

#10 ctroop4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 13

Re: Alternating vowels and consonants

Posted 17 February 2013 - 03:07 PM

View Postpbl, on 17 February 2013 - 02:46 PM, said:

Yes I used your example to show up the logic.
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 
				
	}
}
		
		
	

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

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;
       }
    }



Was This Post Helpful? 1
  • +
  • -

#12 ctroop4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-February 13

Re: Alternating vowels and consonants

Posted 17 February 2013 - 03:37 PM

View Postpbl, 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?
Was This Post Helpful? 0
  • +
  • -

#13 ccdan  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 09-December 12

Re: Alternating vowels and consonants

Posted 17 February 2013 - 04:34 PM

View Postctroop4ever, on 18 February 2013 - 12:37 AM, said:

so i dont have to put the string into a char array?

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

Was This Post Helpful? 2
  • +
  • -

#14 ccdan  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 09-December 12

Re: Alternating vowels and consonants

Posted 17 February 2013 - 04:56 PM

Here are two methods that do the trick:

/** 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

Was This Post Helpful? 1
  • +
  • -

#15 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 92
  • View blog
  • Posts: 276
  • Joined: 07-November 10

Re: Alternating vowels and consonants

Posted 17 February 2013 - 05:05 PM

OR one can program as

Spoiler

This post has been edited by burakaltr: 17 February 2013 - 10:58 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2