473,323 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

how to return a value true if a given char is a vowel and false if not?

i made a program in else-if statement about this problem.
but my teacher requires to give a return statement about this. i am new with in programming and we had just tackle a return statement and i find it hard to understand. please can u help me. or just give me an idea how to do it.
#include <stdio.h>
#include <conio.h>
int main ()
{
char a,e,i,o,u;
char letter,ans;
printf("\n\nenter another letter(1=yes,0=no)?");
scanf("%d",&ans);
if (ans==1){
printf("\nEnter a letter:");
scanf("%d",&letter);
if (letter == 'a'){
printf("true");}
else if (letter == 'e'){
printf("true");}
else if (letter == 'i'){

printf("true");
}
else if (letter == 'o'){

printf("true");
}

else if (letter == 'u'){

printf("true");

}
else
{
printf("false");
}
while (ans==1)

getche ();
return 0;}
}
this is my program in if-else staement.
Aug 31 '09 #1
12 10214
JosAH
11,448 Expert 8TB
I'd do it like this:

Expand|Select|Wrap|Line Numbers
  1. int isVowel(char c) {
  2.    return strchr("aeiou", c) != null;
  3. }
  4.  
For an explanation read this link.

kind regards,

Jos
Aug 31 '09 #2
donbock
2,426 Expert 2GB
@phelle25
Pull lines 12-23 into a separate function called something like LetterIsVowel. This new function accepts a single input argument (the letter to be tested) and provides an integer output, where 1 means true and 0 means false. Replace the printf statements with return statements. Then modify main to use this new function. Have main print either "true" or "false" based on the return value.

By the way, it helps if you use CODE tags for source code snippets.
Aug 31 '09 #3
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#define vowels "aeiou"



main ()
{
char isvowel(char vowels);

char letter;
char letter1;
char ans;


do
{
printf("\n\nwant to enter another letter(1=yes,0=No)?");
scanf("%d",&ans);
if (ans==1)
printf("\nEnter letter:");
scanf("%c",&letter);
letter1=isvowel(letter);
printf("\nletter is: %c",letter1);
}
while (ans==1);
}
char isvowel(char vowels)
{
char letter2;

if (letter2 == 'a'){

return ('T');}


else if (letter2 == 'e'){

return ('T');}


else if (letter2 == 'i'){

return ('T');
}

else if (letter2 == 'o'){

return ('T');
}

else if (letter2 == 'u'){

return ('T');






}
else
{
return ('F');
}

}
this is my return statement. but still something is wrong in the part char isvowel(char vowels); can u help me with this.
Sep 1 '09 #4
JosAH
11,448 Expert 8TB
Have you read (and understood) my reply #2?

kind regards,

Jos
Sep 1 '09 #5
donbock
2,426 Expert 2GB
@phelle25
What do you pass as an argument when you call isvowel?
What is the argument used for in the definition of function isvowel?

Please use CODE tags! Compare the appearance of the source code in post #1 to the same code in post #3.
Sep 1 '09 #6
whodgson
542 512MB
Have you you given any thought to using 'switch (char)' and 'case' where the cases would be each vowel (e.g case: 'a') and would print "true". The default case would print"not a vowel"or "false".
Sep 2 '09 #7
JosAH
11,448 Expert 8TB
@whodgson
Why is everybody ignoring my reply #2?

kind regards,

Jos ;-)
Sep 2 '09 #8
donbock
2,426 Expert 2GB
@JosAH
I stand in awe as I gape agog at reply #2. ;-}

Seriously, my interpretation of the OP's messages is that the fundamental issue is how to write a function and how to invoke it. Making isvowel more efficient is beside the point.

The OP has bigger fish to fry so I wasn't going to bother him with this ... but I can't hold it in any longer. The C Standard reserves certain name patterns for possible use by future versions of the Standard. One of those reserved patterns is the regular expression "^is[a-z]" -- that is, a name that starts with "is" followed by a lowercase letter. The name "isvowel" impinges on this reserved pattern. The OP need not worry about changing the function name; this is not a program that will be used for years and years. Its when you write software that will be maintained for 10+ years that you are advised to be careful of these reserved name patterns.
Sep 2 '09 #9
JosAH
11,448 Expert 8TB
@donbock
That's why I wrote isVowel( ... ) with an uppercase V, just to be safe ;-)

kind regards,

Jos
Sep 2 '09 #10
whodgson
542 512MB
@ JosAH
I don`t think that your link explains your solution very well... at least my first shot at applying it to what you wrote left me baffled (failed). Could you amplify a bit. I will dig further.
EDIT:
Tchtch Jos.... first your code would not compile because of null in lower case and then it declared AEIOU were not vowels!
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3. int isVowel(char c); 
  4.  
  5. int main()
  6. {
  7. char c;    
  8. cout<<"Enter a character, call isVowel() and find if \n"
  9. <<" TRUE or FALSE\n";
  10. cout<<"Enter a character ";
  11. cin>>c;
  12. if(isVowel(c)==1)
  13. cout<<"The character you entered is a vowel \n";
  14. else cout<<"The character you entered is not a vowel \n";
  15. cout<<endl<<endl;
  16. system("pause");
  17. return 0;
  18. }
  19.  
  20. int isVowel(char c) 
  21.    return strchr("aeiou", c) != NULL;// was null; 
Sep 10 '09 #11
hi,
thank u very much for the time and suggestions. actually i got the answer now and i passed th assignment. here it is:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

bool isvowel(char var);

main(){
char v;
bool r;
printf("\n\n\tEnter a Letter: ");
scanf("%c",&v);
r = isvowel(v);

if(r == true) {printf("\nLetter %c is a vowel", v);}
else printf("\nLetter %c is not a vowel",v);

getche();


}

bool isvowel(char var){
bool e;

if(var=='a'||var=='A'||var=='e'||var=='E'||var=='i '||var=='I'||var=='o'||var == 'O'||var=='u'||var=='U')
{
e = true;
}
else e = false;

return e;


}

God bless
Sep 10 '09 #12
donbock
2,426 Expert 2GB
Good for you!

I hope you'll be patient with me for a few pedantic observations.
  1. Which of your headers defined the bool type?
  2. If you're not using C99 and <stdbool.h>, then it is advisable to avoid comparing a bool variable directly to true -- as in if(r == true). There is only one false value (0), but all nonzero values are true. Your variable might have a different nonzero value than "true". I would replace this line with either if(r) or if(r != false).
  3. Did you know you could replace the bulk of isvowel by return ((var=='a')||...||(var=='U'));? This construction is equivalent to your code.
  4. The C Standard mandates that main return an int. In fact, your main function has an implicit int return value. Explicit is usually better than implicit. Either way, you need a return statement at the end of main.
  5. Regarding the problem specification, should "Y" be considered a vowel? The vowel list I learned so many years ago was "A, E, I, O, U, and sometimes Y". If you're feeling whimsical, you could randomly choose between vowel or nonvowel if (and only if) the input character is "Y".

You have a working program, so don't change it. These observations are to help with your next project.

Cheers,
don
Sep 10 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Bob | last post by:
Hi, I have a std::vector, say myVec, of some user defined object, say myOb. In my code, I have a function that searches myVec for a particular myOb. The way I was doing this was searching...
18
by: deanbrown3d | last post by:
I mean, is this correct? try { Screen->Cursor = crHourglass; Do something bad return false; else return true; }
8
by: aundro | last post by:
Hello all, I was wondering whether I could do something like: --snip-- MyType.prototype.myProp = {var xxx = 'this is a string'; xxx.substring(4);} --snip--
1
by: WLF | last post by:
I have the following function (C# behind aspx page): private void ButtonNewSupplier_Click(object sender, System.EventArgs e) { Response.Write("<script...
5
by: siaj | last post by:
Hello, I have a javascript function for a validation in the HTML page of the asp.Net page.. I call this function in a Savebutton click When the validation fails No postback should happen ( ie...
11
by: randomtalk | last post by:
hi, i have the following recursive function (simplified to demonstrate the problem): >>> def reTest(bool): .... result = .... if not bool: .... reTest(True) .... else: .... print...
9
by: Water Cooler v2 | last post by:
Is it necessary to return a value from the event handlers? For instance, what does the return value in the following code signify? What will be its impact if it returned otherwise (true)? <a...
3
by: Doug | last post by:
Hi i have a method that returns a value public bool readxml (string xmlFilename, out string value) but I would like to catch an exception if it occurs in the method . How do i catch the...
9
by: Jamey Bon | last post by:
As a newbie to C#, I am not sure what I can do about this. I would like to do something like an Enumeration to use "constants" like Yes to indicate true and No for false. But since there seems to...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.