29. write a C Program to count number of vowels, consonants, digits, words in a given file


Program to count number of vowels, consonants, digits, words in a given file

Source Code:

#include <stdio.h>
#include<ctype.h>
int main()
{
  char line[150];
  FILE *ptr;
  int vowels, consonant, digit, space,words,specialCharacters;
  vowels = consonant = digit = space = specialCharacters=0, words =1;
  ptr=fopen("/home/smec/Desktop/hello.txt","r");
  fgets(line, sizeof(line), ptr);
if(ptr!=NULL)
{
  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] == ' ')
        {
space++;
    words++;
    }
    else if (line[i] >= '0' && line[i] <= '9')
    {
     ++digit;
    }
    else
        {
            ++specialCharacters;
        }
  }
}
else{
printf("file Unable to open");
}
  printf("Vowels: %d", vowels);
  printf("\nConsonants: %d", consonant);
  printf("\nDigits: %d", digit);
  printf("\nWhite spaces: %d", space);
  printf("\nWords: %d", words);
  printf("\nspecialCharacters: %d", specialCharacters);
  return 0;
}

Output:

Vowels: 5
Consonants: 11
Digits: 3
White spaces: 3
Words: 4
specialCharacters: 3

hello.txt




No comments:

Post a Comment