I have to write a program that selects consonants from a string and counts them, and shows how many times each of them appeared. I wrote this code, but it also counts vowels. I don't know why.
#include <stdio.h>
int main()
{
char str[100];
int i, consonants;
i = consonants = 0;
int freq[256] = {0};
printf("\n Please Enter any String : ");
scanf("%[^\n]", str);
while (str[i] != '\0')
{
if ((str[i] >= 'a' && str[i] <= 'z') ||
(str[i] >= 'A' && str[i] <= 'Z') &&
(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'))
{
consonants++;
freq[str[i]]++;
}
i++;
}
printf("\n Number of Consonants in this String = %d", consonants);
for (i = 0; i < 256; i++)
{
if (freq[i] != 0)
{
printf("\nThe frequency of %c is %d", i, freq[i]);
}
}
return 0;
}
This is output:
Please Enter any String : We strive to achive peace
Number of Consonants in this String = 21
The frequency of W is 1
The frequency of a is 2
The frequency of c is 2
The frequency of e is 5
The frequency of h is 1
The frequency of i is 2
The frequency of o is 1
The frequency of p is 1
The frequency of r is 1
The frequency of s is 1
The frequency of t is 2
The frequency of v is 2