5 Replies - 10956 Views - Last Post: 10 October 2012 - 12:43 PM Rate Topic: -----

#1 wjmsi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-March 10

function isVowel

Posted 07 March 2010 - 05:26 PM

how do use the function isVowel, and also what would be the correct syntax for it?
Is This A Good Question/Topic? 0
  • +

Replies To: function isVowel

#2 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: function isVowel

Posted 07 March 2010 - 05:35 PM

View Postwjmsi, on 07 March 2010 - 04:26 PM, said:

how do use the function isVowel, and also what would be the correct syntax for it?


As far as i know, there isn't a isVowel in C++ standard lib, however, it would be easy to implement. Something like this :D
bool isVowel(char ch) {
    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
         return true;
    else return false;
}


Hope i helped
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: function isVowel

Posted 07 March 2010 - 08:23 PM

@athlon32 -- Oh you made the faux pas! Never use an if-statment or a ternary operator to convert a Boolean expression into a Boolean value -- it is redundant!

your code is just this:

bool isVowel(char ch) { return (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'); }

There is no need for the if-statement. The expression resolves to either true, or false -- so the return value can just be the result of the expression.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: function isVowel

Posted 07 March 2010 - 08:59 PM

just to add,
it may be handy to convert to uppercase first to cover all cases
bool isVowel(char ch) 
{ 
	ch = toupper(ch);
	return (ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U'); 
}


Was This Post Helpful? 0
  • +
  • -

#5 sganesh_08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-February 10

Re: function isVowel

Posted 08 March 2010 - 02:26 AM

Actually, while checking the vowel we should not change that character in that function.

The main purpose of this function only checking.

bool isVowel(char ch) 
{ 
       return ( ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U'); 
}



And it has better performance than other. Because, in toupper function they will check all the condition for finding whether it is upper case or not. Then only they will change that character.
Was This Post Helpful? 0
  • +
  • -

#6 Rumsy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 30-January 11

Re: function isVowel

Posted 10 October 2012 - 12:43 PM

View Postsganesh_08, on 08 March 2010 - 02:26 AM, said:

Actually, while checking the vowel we should not change that character in that function.

The main purpose of this function only checking.

bool isVowel(char ch) 
{ 
       return ( ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U'); 
}



And it has better performance than other. Because, in toupper function they will check all the condition for finding whether it is upper case or not. Then only they will change that character.


I am posting because I found this entire topic helpful and jjl was correct; it is more helpful to convert to uppercase. The function calls 'ch' by value, getting a copy of it which can then be converted to uppercase and checked against a smaller list in the return statement. The boolean function has no side effect. This saves more time typing out an "isConsonant" function as well. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1