lab 2 CD
lab 2 CD
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