You are on page 1of 7

CSE2010 – ADVANCED C PROGRAMMING

L25+L26
Lab Assessment 2
Name: Ajayjith.N.S
Reg.no:18BCE2332

Questions:
1. Write a c program to read multiple lines of a text as individual strings who's max length is unspecified
maintain a pointer to each string within a one dimensional array of pointers then determine the no. of vowels
consonants, digits ,white spaces and other characters of each line finally determine the avg no. Vowels per line
and consonants per line.
2. Write a c program to calculate no. of the days between two given dates.

Solutions:
1.
CODE:
#include <stdio.h>
int main() {

char line[150];
int vowels, consonant, digit, space;
vowels = consonant = digit = space = 0;
printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);

for (int i = 0; line[i] != '\0'; ++i) {

line[i] = tolower(line[i]);
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u') {
++vowels;
}
else if ((line[i] >= 'a' && line[i] <= 'z')) {
++consonant;
}
else if (line[i] >= '0' && line[i] <= '9') {
++digit;
}
else if (line[i] == ' ') {
++space;
}
}

printf("Vowels: %d", vowels);


printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);

return 0;
}
OUTPUT:

2.
CODE:
#include<stdio.h>
#include<stdlib.h>
int valid_date(int date, int mon, int year);
int main(void)
{
int day1, mon1, year1,
day2, mon2, year2;

int day_diff, mon_diff, year_diff;

printf("Enter start date in the format (MM/DD/YYYY): ");


scanf("%d/%d/%d", &mon1, &day1, &year1);

printf("Enter end date in the format (MM/DD/YYYY): ");


scanf("%d/%d/%d", &mon2, &day2, &year2);

if(!valid_date(day1, mon1, year1))


{
printf("First date is invalid.\n");
}

if(!valid_date(day2, mon2, year2))


{
printf("Second date is invalid.\n");
exit(0);
}

if(day2 < day1)


{

if (mon2 == 3)
{

if ((year2 % 4 == 0 && year2 % 100 != 0) || (year2 % 400 == 0))


{
day2 += 29;
}

else
{
day2 += 28;
}
}

else if (mon2 == 5 || mon2 == 7 || mon2 == 10 || mon2 == 12)


{
day2 += 30;
}

else
{
day2 += 31;
}

mon2 = mon2 - 1;
}

if (mon2 < mon1)


{
mon2 += 12;
year2 -= 1;
}

day_diff = day2 - day1;


mon_diff = mon2 - mon1;
year_diff = year2 - year1;

printf("Difference: %d years %02d months and %02d days.", year_diff, mon_diff, day_diff);

return 0;
}

int valid_date(int day, int mon, int year)


{
int is_valid = 1, is_leap = 0;

if (year >= 1800 && year <= 9999)


{

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


{
is_leap = 1;
}

if(mon >= 1 && mon <= 12)


{

if (mon == 2)
{
if (is_leap && day == 29)
{
is_valid = 1;
}
else if(day > 28)
{
is_valid = 0;
}
}

else if (mon == 4 || mon == 6 || mon == 9 || mon == 11)


{
if (day > 30)
{
is_valid = 0;
}
}

else if(day > 31)


{
is_valid = 0;
}
}

else
{
is_valid = 0;
}

}
else
{
is_valid = 0;
}

return is_valid;
}

OUTPUT:

You might also like