5) Write a function to determine is a given word is legal. A word is illegal if
ID: 3758366 • 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
Examples:
sch - is illegal, no vowels
apple - legal, contains a vowel
APPle - legal, uppercase letter combinations do not matter
try - legal, no vowel, but contains the letter 'y'
Explanation / Answer
1)
1. Macro for finding area of a circle:
#include <stdio.h>
#define PI 3.1415
#define area(r) (PI*(r)*(r))
int main(){
int radius;
float area;
printf("Enter the radius: ");
scanf("%d",&radius);
area=area(radius);
printf("Area=%.2f",area);
return 0;
}
2. Macro for finding area of a Square:
#include <stdio.h>
#define area(a) ((a)*(a))
int main(){
int length;
float area;
printf("Enter the length: ");
scanf("%d",&length);
area=area(length);
printf("Area=%.2f",area);
return 0;
}
3. Macro for finding area of a rectangle:
#include <stdio.h>
#define area(a,b) ((a)*(b))
int main(){
int len,breadth;
float area;
printf("Enter the length and breadth : ");
scanf("%d%d",&len,&breadth);
area=area(len,breadth);
printf("Area=%.2f",area);
return 0;
}
4. Macro for finding area of a Triangle:
#include <stdio.h>
#define area(x,y) (0.5*x*y)
int main(){
int base,height;
float area;
printf("Enter the base and height : ");
scanf("%d%d",&base,&height);
area=area(base,height);
printf("Area=%.2f",area);
return 0;
}
2. program which counts no of words in a string
#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int words = 0, len = 0, i;
clrscr();
printf(" ENTER A STRING...: ");
gets(str);
while(str[len]!='')
len++;
len--;
for(i=0;i<=len;i++)
{
if((str[i] == ' ' && str[i+1] != ' ') || i == len)
words++;
}
printf(" NUMBER OF WORDS IN THE ABOVE SENTENCE IS...: %d", words);
getch();
}
Program to determine if that date is valid.
#include <stdio.h>
#include <ctype.h>
int validDate(int month, int day, int year)
{
int validity;
int rem_4, rem_100, rem_400;
if(year%4== 0)
rem_4=1;
else if (year%100 == 0)
rem_100=1;
elseif(year%400 == 0)
rem_400=1;
if (rem_4==1||rem_100==1||rem_400==1)
printf("it is a leap year ");
else
printf("not a leap year ");
if(month>=1&&month<=12)
if(day>=1&&day<=31)
if(year>1581)
validity = 1;
else
validity=0;
return validity;
}
int main(void)
{
int month, day, year, validState;
printf(" Enter month: ");
scanf("%i ", &month);
printf(" Enter day: ");
scanf("%i ", &day);
printf(" Enter year: ");
scanf("%i ", &year);
validState = validDate(month, day, year);
if (validState == 1)
printf(" The date you entered is VALID ");
else
printf(" The date you entered is INVALID ");
}
4.
#include<stdio.h>
#include<conio.h>
#define SIZE 50
int card_score(int num_of_cards)
{
char card_value[SIZE];
int ace_seen=0, total=0;
char temp[20];
printf("enter no of cards ");
scanf("%d",num_of_cards);
printf("enter cards ");
for (int i=0; i<num_of_cards;i++)
{
scanf("%s", &card_value[i])
temp=card_value[i];
switch (temp)
{
case 'a': case 'A':
total +=11;
ace_seen++;
break;
case 'k': case 'K':
case 'q': case 'Q':
case 'j': case 'J':
case 't': case 'T':
total +=10;
break;
case '9':
total +=9;
break;
case '8':
total +=8;
break;
case '7':
total +=7;
break;
case '6':
total +=6;
break;
case '5':
total +=5;
break;
case '4':
total +=4;
break;
case '3':
total +=3;
break;
case '2':
total +=2;
break;
default:
printf("Invalid cards. Please try again.");
break;
}
}
return total;
}
int main()
{
char card_value[SIZE];
int num_of_cards = 0;
int total;
int i = 0;
printf(" Enter cards: ");
scanf("%c", &card_value[i]);
total = card_score(num_of_cards);
printf(" Your score is %d: ",total);
return 0;
}
5.
wheather word is illegal or not:
#include <stdio.h>
void main()
{
char word[30];
int i, vowels = 0;
printf("Enter a word ");
gets(word);
for (i = 0; word[i] != ''; i++)
{
if ((word[i] == 'a' || word[i] == 'e' || word[i] ==
'i' || word[i] == 'o' || word[i] == 'u') ||word[i]='y')
(word[i] == 'A' || word[i] == 'E' || word[i] ==
'I' || word[i] == 'O' || word[i] == 'U'||word[i]='Y'))
{
vowels = vowels + 1;
}
}
if(vowels==0)
printf("entered word is illegal ");
else
printf("entered word is legal ");
}
6.
/* C program to find whether given string is a palindrome or not*/
#include <stdio.h>
#include <string.h>
void main()
{
char string[25], reverse_string[25] = {''};
int i, length = 0, flag = 0;
printf("Enter a string ");
gets(string);
for (i = 0; string[i] != ''; i++)
{
length++;
}
printf("The length of the string '%s' = %d ", string, length);
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome ", string);
else
printf("%s is not a palindrome ", string);
}
7.
#include <stdio.h>
#include <string.h>
#include<ctype.h>
struct info
{
long length;
int upper_case;
int lower_case;
int digits;
int non_alphanumeric;
};
void string_info (struct info s_info[], char string []);
int main()
{
struct info s_info [1];
char string [40];
printf("Please enter a string: ");
scanf("%s", & string);
string_info (s_info, string);
return(0);
}
void string_info (struct info s_info[], char string [])
{
int i;
s_info.length = strlen(string);
for (i = 0; i < strlen(string); ++i)
{
s_info.upper_case += (isupper(string[i])) ? 1 : 0;
s_info[i].lower_case+ =( islower(string[i])) ?1:0 ;
s_info[i].digits += (isdigit(string[i]))?1:0;
s_info[i].non_alphanumeric+=(isalnum(string[i]))? 1:0;
}
printf(" The length of the string is %li: ", s_info[i].length);
printf(" The number of upper case letters in the string is %i: ", s_info[i].upper_case);
printf(" The number of lower case letters in the string is %i: ", s_info[i].lower_case);
printf(" The number of digits in the string is: %i ", s_info[i].digits);
printf(" The number of alphanumeric in the string is: %i ", s_info[i].non_alphanumeric);
printf(" The number of non-alphanumeric is :%d",s_info[i].length-s_info[i].non_alphanumeric);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.