Checking Vowels

checking vowels in one line rather using functions...

Page 1 of 1

14 Replies - 9413 Views - Last Post: 21 August 2009 - 05:14 PM Rate Topic: -----

#1 krazyrohil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-August 09

Checking Vowels

Post icon  Posted 14 August 2009 - 09:47 AM

Can anyone please help me by telling how to check whether a character is a vowel just in one line???
i mean i dont want to use an user defined function ( such as boolean isVowel) or check it using an array...i just want a one line code...if anyone can help me plz reply...
Is This A Good Question/Topic? 0
  • +

Replies To: Checking Vowels

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12044
  • View blog
  • Posts: 44,902
  • Joined: 27-December 08

Re: Checking Vowels

Posted 14 August 2009 - 11:38 AM

View Postkrazyrohil, on 14 Aug, 2009 - 10:47 AM, said:

Can anyone please help me by telling how to check whether a character is a vowel just in one line???
i mean i dont want to use an user defined function ( such as boolean isVowel) or check it using an array...i just want a one line code...if anyone can help me plz reply...


[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Checking Vowels

Posted 14 August 2009 - 11:55 AM

I would use the charAt() function with a String.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8375
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Checking Vowels

Posted 14 August 2009 - 04:36 PM

This is completly againts Steve McConnell Code Complete Second Edition
Chapter #19.1 Boolean Expressions

Move complicated expressions into boolean functions
If a test is repeated often or distracts from the main flow of the program, move the code for the test into a function and test the value of the function. For example, here's a complicated test:

Visual Basic Example of a Complicated Test
If ( ( document.AtEndOfStream ) And ( Not inputError ) ) And _

   ( ( MIN_LINES <= lineCount ) And ( lineCount <= MAX_LINES ) ) And _

   ( Not ErrorProcessing( ) ) Then

   ' do something or other

   ...

End If



This is an ugly test to have to read through if you're not interested in the test itself. By putting it into a boolean function, you can isolate the test and allow the reader to forget about it unless it's important. Here's how you could put the if test into a function:

Visual Basic Example of a Complicated Test Moved into a Boolean Function, with New Intermediate Variables to Make the Test Clearer
Function DocumentIsValid( _

   ByRef documentToCheck As Document, _

   lineCount As Integer, _

   inputError As Boolean _
   ) As Boolean

   Dim allDataRead As Boolean
   Dim legalLineCount As Boolean

   allDataRead = ( documentToCheck.AtEndOfStream ) And ( Not inputError )	   <-- 1

   legalLineCount = ( MIN_LINES <= lineCount ) And ( lineCount <= MAX_LINES )	   <-- 1

   DocumentIsValid = allDataRead And legalLineCount And ( Not ErrorProcessing() )

End Function


Was This Post Helpful? 1
  • +
  • -

#5 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Checking Vowels

Posted 14 August 2009 - 06:58 PM

View Postkrazyrohil, on 14 Aug, 2009 - 08:47 AM, said:

Can anyone please help me by telling how to check whether a character is a vowel just in one line???
i mean i dont want to use an user defined function ( such as boolean isVowel) or check it using an array...i just want a one line code...if anyone can help me plz reply...



you can try isVowel() or you would have to manually write a short boolean method using compareTo( vowels )


hope that helps...
Was This Post Helpful? 0
  • +
  • -

#6 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Checking Vowels

Posted 14 August 2009 - 08:05 PM

boolean isVowel = false;
char toTest = ...
isVowel = (Arrays.binarySearch({'a'...'u'}, toTest) >=0 );


should work. There are better ways but it is one line. ( not sure if I initialized the array the right way... its been awhile.
Was This Post Helpful? 0
  • +
  • -

#7 LynnL  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 109
  • Joined: 13-April 09

Re: Checking Vowels

Posted 14 August 2009 - 08:12 PM

View Postpbl, on 14 Aug, 2009 - 03:36 PM, said:

This is completly againts Steve McConnell Code Complete Second Edition
Chapter #19.1 Boolean Expressions

Move complicated expressions into boolean functions
If a test is repeated often or distracts from the main flow of the program, move the code for the test into a function and test the value of the function. For example, here's a complicated test:

Visual Basic Example of a Complicated Test
If ( ( document.AtEndOfStream ) And ( Not inputError ) ) And _

   ( ( MIN_LINES <= lineCount ) And ( lineCount <= MAX_LINES ) ) And _

   ( Not ErrorProcessing( ) ) Then

   ' do something or other

   ...

End If



This is an ugly test to have to read through if you're not interested in the test itself. By putting it into a boolean function, you can isolate the test and allow the reader to forget about it unless it's important. Here's how you could put the if test into a function:

Visual Basic Example of a Complicated Test Moved into a Boolean Function, with New Intermediate Variables to Make the Test Clearer
Function DocumentIsValid( _

   ByRef documentToCheck As Document, _

   lineCount As Integer, _

   inputError As Boolean _
   ) As Boolean

   Dim allDataRead As Boolean
   Dim legalLineCount As Boolean

   allDataRead = ( documentToCheck.AtEndOfStream ) And ( Not inputError )	   <-- 1

   legalLineCount = ( MIN_LINES <= lineCount ) And ( lineCount <= MAX_LINES )	   <-- 1

   DocumentIsValid = allDataRead And legalLineCount And ( Not ErrorProcessing() )

End Function


everybody should read that book
Was This Post Helpful? 0
  • +
  • -

#8 krazyrohil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-August 09

Re: Checking Vowels

Posted 15 August 2009 - 03:40 AM

View Postcomputerfox, on 14 Aug, 2009 - 05:58 PM, said:

View Postkrazyrohil, on 14 Aug, 2009 - 08:47 AM, said:

Can anyone please help me by telling how to check whether a character is a vowel just in one line???
i mean i dont want to use an user defined function ( such as boolean isVowel) or check it using an array...i just want a one line code...if anyone can help me plz reply...



you can try isVowel() or you would have to manually write a short boolean method using compareTo( vowels )


hope that helps...


i know how to make a function boolean isVowel() but i want to do this wuithout using a user function like this...can it be done by using some other in built function or something without use any variable as such
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8375
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Checking Vowels

Posted 15 August 2009 - 08:01 PM

View Postkrazyrohil, on 15 Aug, 2009 - 02:40 AM, said:

i know how to make a function boolean isVowel() but i want to do this wuithout using a user function like this...can it be done by using some other in built function or something without use any variable as such

Why ? You are allergic to them ? Or you missed the whole point ?
Was This Post Helpful? 0
  • +
  • -

#10 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Checking Vowels

Posted 15 August 2009 - 08:07 PM

Quote

i know how to make a function boolean isVowel() but i want to do this wuithout using a user function like this...can it be done by using some other in built function or something without use any variable as such

Quote

boolean isVowel = false;
char toTest = ...
isVowel = (Arrays.binarySearch({'a'...'u'}, toTest) >=0 );


should work. There are better ways but it is one line. ( not sure if I initialized the array the right way... its been awhile.


While I agree with pbl that a boolean method would be much clearer, it is one line as you requested. The first two lines are simply initialization of variables.

This post has been edited by syfran: 15 August 2009 - 08:10 PM

Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6788
  • View blog
  • Posts: 14,235
  • Joined: 16-October 07

Re: Checking Vowels

Posted 16 August 2009 - 01:57 AM

One line boolean method? :P

boolean isVowelCheck(char ch) { return "AEIOUaeiou".indexOf(ch)!=-1; }


Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8375
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Checking Vowels

Posted 16 August 2009 - 06:13 PM

View Postbaavgai, on 16 Aug, 2009 - 12:57 AM, said:

One line boolean method? :P

boolean isVowelCheck(char ch) { return "AEIOUaeiou".indexOf(ch)!=-1; }


:^:
Was This Post Helpful? 0
  • +
  • -

#13 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: Checking Vowels

Posted 16 August 2009 - 06:23 PM

View Postpbl, on 16 Aug, 2009 - 05:13 PM, said:

View Postbaavgai, on 16 Aug, 2009 - 12:57 AM, said:

One line boolean method? :P

boolean isVowelCheck(char ch) { return "AEIOUaeiou".indexOf(ch)!=-1; }


:^:



yeah, makes complete sense :P

:^: :^:
Was This Post Helpful? 0
  • +
  • -

#14 krazyrohil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-August 09

Re: Checking Vowels

Posted 21 August 2009 - 01:40 AM

View Postpbl, on 15 Aug, 2009 - 07:01 PM, said:

View Postkrazyrohil, on 15 Aug, 2009 - 02:40 AM, said:

i know how to make a function boolean isVowel() but i want to do this wuithout using a user function like this...can it be done by using some other in built function or something without use any variable as such

Why ? You are allergic to them ? Or you missed the whole point ?



it is not about being allergic PBL...it is only that i need to do it without making a function in OOPS style...i hope you know what OOPS style means...or rather in Object Oriented Style.....
thats all why i want to do it withou making a boolean function...
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8375
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Checking Vowels

Posted 21 August 2009 - 05:14 PM

View Postkrazyrohil, on 21 Aug, 2009 - 12:40 AM, said:

it is not about being allergic PBL...it is only that i need to do it without making a function in OOPS style...i hope you know what OOPS style means...or rather in Object Oriented Style.....
thats all why i want to do it withou making a boolean function...


You really missed the whole point :)

You really want to be OOP ? Then you should do something like that

class MyChar {
	 
	  char c;

	  // constructor
	  MyChar(char c) {
		  this.c = c;
	  }

	  boolean isVoyel() {
		 switch(Character.toUpperCase(c)) {
			case 'A':
			case 'E':
			case 'I':
			case 'O':
			case 'U:
			case 'Y':			  // yes in French
				return true;
		 }
		 return false;
	   }

	   public String toString() {
		  return new String(c);
	   }
}



   String str = "This is a sentence";
   char[] digit = str.toCharArray();
  
	for(int i = 0; i < digit.length; i++) {
	   MyChar mc = new MyChar(digit[i]);
	   System.out.println(mc + " is a voyel " + mc/isVoyel());
   }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1