In: Computer Science
Here is a Problem I need to solve:
5) Write a function to determine is a given word is legal. A
word is illegal if it contains no vowels. For this problem,
the letter Y is considered to be a legal vowel. Pass in a word to
this function and it will determine if the word is
legal or not. You can make the following assumptions about the word
you a passing to this function.
1) The string being passed is a combination of letters only (no
non-letter check needed)
2) The string being passed is null terminated
3) Letters may be capital or lower case and it has no effect on
whether its a word
I think I am in the right direction below. But the problem is my proposed solution involves two separate functions. My question is how I can make this one function?
int isVowel(char c){
if(c>='A' && c<='Z')
c=c+32;
if(c=='a' || c=='e' || c=='i'|| c=='o'|| c=='u'|| c=='y')
return 1;
else
return 0;
}
int validString(char *c){
int i;
for(i=0;c[i]!='\0';i++)
if(isVowel(c[i]))
return 1;
return 0;
}
input code:
output:
code:
#include <stdio.h>
int validString(char *c)
{
/*declare the variables*/
int i;
/*check the characters*/
for(i=0;c[i]!='\0';i++)
{
/*check vowel or not*/
if(*(c+i)=='a' || *(c+i)=='e' || *(c+i)=='i'|| *(c+i)=='o'||
*(c+i)=='u'|| *(c+i)=='y')
{
//return true if vowel
return 1;
}
}
/*else return FALSE*/
return 0;
}
int main()
{
/*declare the variables*/
char word[10];
/*take user input*/
printf("Enter a word:");
scanf("%s",word);
/*call the function*/
if(validString(word))
{
/*print Legal*/
printf("Legal word");
}
else
{
/*print Inlegal*/
printf("illegal word");
}
return 0;
}