5) Write a function to determine is a given word is legal. A word is illegal if
ID: 3760768 • Letter: 5
Question
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
Explanation / Answer
1)
int validDate(int d, int m ,int y)
{
int leap=0, valid = 0;
if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
leap=1;
if (m<13)
{
if (m == 1 || m == 3 || m==5 || m==7 || m== 8 || m== 10 || m== 12)
if (d <=31)
valid=1;
else if (m == 4 || m== 6 || m==8 || m==10 )
if (d <= 30)
valid = 1;
else
{
if (leap == 1)
if (d <= 29)
valid = 1;
if (leap == 0)
if (d <= 28)
valid = 1;
}
}
if (valid==1)
return 1;
else
return 0;
}
2)
#include <stdio.h>
int isvalid(char c1, char c2);
int sum(char c1, char c2);
int main()
{
char c1, c2;
scanf("%c %c",&c1,&c2);
if(isvalid(c1,c2))
printf("%d ",sum(c1,c2));
else
printf("Invalid input");
return 0;
}
int sum(char c1, char c2)
{
int res=0;
if(c1=='A' && c2=='A')
return 12;
if(c1<='9' && c1>='2')
res+=c1-'0';
if(c1=='T' || c1 == 'J' || c1=='Q' || c1=='K')
res+=10;
if(c1=='A')
res+=11;
if(c2<='9' && c2>='2')
res+=c2-'0';
if(c2=='T' || c2 == 'J' || c2=='Q' || c2=='K')
res+=10;
if(c2=='A')
res+=11;
return res;
}
int isvalid(char c1, char c2)
{
int i,cnt=0;
char a[]="23456789ATJQK";
for(i=0;a[i];i++)
{
if(c1==a[i])
cnt++;
if(c2==a[i])
cnt++;
}
if(cnt==2)
return 1;
return 0;
}
3)
int validDate(int d, int m ,int y)
{
int leap=0, valid = 0;
if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
leap=1;
if (m<13)
{
if (m == 1 || m == 3 || m==5 || m==7 || m== 8 || m== 10 || m== 12)
if (d <=31)
valid=1;
else if (m == 4 || m== 6 || m==8 || m==10 )
if (d <= 30)
valid = 1;
else
{
if (leap == 1)
if (d <= 29)
valid = 1;
if (leap == 0)
if (d <= 28)
valid = 1;
}
}
if (valid==1)
return 1;
else
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.