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

lab 2 CD

The document presents a C program designed to count the number of digits, vowels, consonants, symbols, and spaces in a text file named 'sample.txt'. It includes the program code, which reads the file, processes the characters, and outputs the counts of each category. Additionally, it specifies the context of the practical exercise within a compiler design laboratory course.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lab 2 CD

The document presents a C program designed to count the number of digits, vowels, consonants, symbols, and spaces in a text file named 'sample.txt'. It includes the program code, which reads the file, processes the characters, and outputs the counts of each category. Additionally, it specifies the context of the practical exercise within a compiler design laboratory course.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

FACULTYOF ENGINEERING& TECHNOLOGYSEM_, YEAR:3

COMPILER DESIGN LABORATORY (303105350)


ENROLL NO:2203031240605

PRACTICAL: 2
AIM: Program to count digits, vowels and symbols in C.

PROGRAM CODE : -

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
char str[100];
int i =0;
int vowels = 0, consonant=0, digit=0, symbols=0, spaces=0;
FILE*fp;
char ch;
fp = fopen("sample.txt","r");
if(fp == NULL)
{
printf("file is not found");
exit(1);
}
ch = fgetc(fp);
printf("Your string is:\n");
while(!feof(fp)){
str[i++]=ch;
ch = fgetc(fp);
}
str[i]='\0';
printf("%s", str);
fclose(fp);
for(i=0;str[i]!='\0';i++){

if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u
'||str[i]=='U'){
vowels++;
}
else if((str[i]>='a' && str[i]<='z')||(str[i]>='A' && str[i]<='Z')){
consonant++;
}
else if(str[i]>='0'&& str[i]<='9'){
digit++;
CD (303105350)6AI13/AIDS/CSE/PIET/ 5
FACULTYOF ENGINEERING& TECHNOLOGYSEM_, YEAR:3
COMPILER DESIGN LABORATORY (303105350)
ENROLL NO:2203031240605

}
else if(str[i]==' '){
spaces++;
}
else{
symbols++;
}
}
printf("\nVowels = %d",vowels);
printf("\nConsonant = %d", consonant);
printf("\nDigit = %d", digit);
printf("\nSpecialSymbols = %d", symbols);
printf("\nWhite Spaces = %d", spaces);
return 0;
}

CD (303105350)6AI13/AIDS/CSE/PIET/ 6
FACULTYOF ENGINEERING& TECHNOLOGYSEM_, YEAR:3
COMPILER DESIGN LABORATORY (303105350)
ENROLL NO:2203031240605

OUTPUT :-

CD (303105350)6AI13/AIDS/CSE/PIET/ 7

You might also like