given an input string Ayodhya your C program to count the number of vowels and consonants and print the frequency of occurrence of each vowel and consonant delete the vowels in the same input string and display the updated string
Sample input:
Enter the input string: Ayodhya
Sample output:
No of vowels:3
Frequency of occurrence:A-2;o-1
No of consonants:4
Frequency of occurrence:y-2;d-1;h-1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>//Standart Input Output Stream
#include <stdlib.h>
#include <string.h>//use string-function
#include <ctype.h>
int main()
{
char str[1028];//String
printf("Enter the input string: ");
scanf("\n");
fgets(str, 1028, stdin);//Read line
char vow[6] = { 'a','e','i','o','u','\0' };
int alfabet[26];//frequency of ocurency a-z
for (int i = 0; i < 26; i++)
alfabet[i] = 0;
for (int i = 0; i < strlen(str); i++)
{
alfabet[tolower(str[i]) - 'a']++;//increment frequency
}
int vowCnt = 0;//Count vowels
int consCnt = 0;//Count consonants
for (int i = 0; i < strlen(str); i++)
{
if (isalpha(str[i]))
{
int isVov = 0;
for (int j = 0; j < strlen(vow); j++)
{
if (tolower(str[i]) == vow[j])
{
vowCnt++;
isVov = 1;
break;
}
}
if (!isVov)//if consonants
consCnt++;
}
}
printf("No of vowels:%d\n", vowCnt);
printf("Frequency of occurrence:");
for (int i = 0; i < strlen(str); i++)
{
if (isalpha(str[i]))
{
for (int j = 0; j < strlen(vow); j++)
{
if (tolower(str[i]) == vow[j] && alfabet[tolower(str[i]) - 'a'] != 0)
{
printf("%c-%d;", str[i], alfabet[tolower(str[i]) - 'a']);
alfabet[tolower(str[i]) - 'a'] = 0;
}
}
}
}
printf("\n");
printf("No of consonants:%d\n", consCnt);
printf("Frequency of occurrence:");
for (int i = 0; i < strlen(str); i++)
{
if (isalpha(str[i]))
{
int is = 0;
for (int j = 0; j < strlen(vow); j++)
{
if (tolower(str[i]) == vow[j])
{
is = 1;
}
}
if (!is && alfabet[tolower(str[i]) - 'a'] != 0)
{
printf("%c-%d;", str[i], alfabet[tolower(str[i]) - 'a']);
alfabet[tolower(str[i]) - 'a'] = 0;
}
}
}
//Delete all vowels
for (int i = 0; i < strlen(str); i++)
{
//if char is alpha and vowels
if (isalpha(str[i]))
{
for (int j = 0; j < strlen(vow); j++)
{
if (tolower(str[i]) == vow[j])//delete by shifting all leter
{
for (int k = i; k < strlen(str) - 1; k++)
str[k] = str[k + 1];
str[strlen(str) - 1] = '\0';
}
}
}
}
printf("out: %s\n", str);
return 0;
}
Comments
Leave a comment