0% found this document useful (0 votes)
6 views

Lab Assignment

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab Assignment

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: MD Zahid Hasan

ID: 213-15-4600
Sec: 60_C
Course name: Compiler Design Lab
PROBLEM 1. Write a C program that will take a user input as a String, and print the number of vowels,
consonants, Digits, white Spaces, and Punctuations as follows...
Sample Input:
Hello Class! Today 15th Sept.
Sample Output:
Vowel: 6
Consonant: 15
Digits: 2
WS: 4
Punctuations: 2

Solutions:

#include <stdio.h>
#include <ctype.h>

int main() {
char s[100];
int v = 0, c = 0, d = 0, ws = 0, p = 0;

printf("Enter a string: ");


fgets(s, sizeof(s), stdin);

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


char ch = s[i];

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||


ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
v++;
}
else if (isalpha(ch)) {
c++;
}
else if (isdigit(ch)) {
d++;
}
else if (isspace(ch)) {
ws++;
}
else if (ispunct(ch)) {
p++;
}
}

printf("Vowel: %d\n", v);


printf("Consonant: %d\n", c);
printf("Digits: %d\n", d);
printf("WS: %d\n", ws);
printf("Punctuations: %d\n", p);

return 0;
}
PROBLEM 2. Write a C program that will take a user input as a String, and print all the vowels,
consonants, Digits, and Punctuations as follows...

Sample Input:
Hello Class! Today 15th Sept.
Sample Output:
Vowel: eoaoae
Consonant: HllClssTdythSpt
Digits: 15
Punctuations: !.

Solution:

#include <stdio.h>
#include <ctype.h>

int main() {
char s[100];
char v[100] = "", c[100] = "", d[100] = "", p[100] = "";
int vi = 0, ci = 0, di = 0, pi = 0;

printf("Enter a string: ");


fgets(s, sizeof(s), stdin);

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


char ch = s[i];

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||


ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
v[vi++] = ch;
}
else if (isalpha(ch)) {
c[ci++] = ch;
}
else if (isdigit(ch)) {
d[di++] = ch;
}
else if (ispunct(ch)) {
p[pi++] = ch;
}
}

v[vi] = '\0';
c[ci] = '\0';
d[di] = '\0';
p[pi] = '\0';

printf("Vowel: %s\n", v);


printf("Consonant: %s\n", c);
printf("Digits: %s\n", d);
printf("Punctuations: %s\n", p);

return 0;
}

You might also like