0% found this document useful (0 votes)
29 views2 pages

C Style Count The Number of Vowels, Consonants Etc and Frequency of Characters in A String

The document contains two C programs, the first counts vowels, consonants, digits and spaces in a string, the second counts the frequency of a given character in a string.

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

C Style Count The Number of Vowels, Consonants Etc and Frequency of Characters in A String

The document contains two C programs, the first counts vowels, consonants, digits and spaces in a string, the second counts the frequency of a given character in a string.

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Program to count vowels, consonants, etc.

#include <stdio.h>
int main() {

char line[150];
int vowels, consonant, digit, space;

// initialize all variables to 0


vowels = consonant = digit = space = 0;

// get full line of string input


printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);

// loop through each character of the string


for (int i = 0; line[i] != '\0'; ++i) {

// convert character to lowercase


line[i] = tolower(line[i]);

// check if the character is a vowel


if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u') {

// increment value of vowels by 1


++vowels;
}

// if it is not a vowel and if it is an alphabet, it is a consonant


else if ((line[i] >= 'a' && line[i] <= 'z')) {
++consonant;
}

// check if the character is a digit


else if (line[i] >= '0' && line[i] <= '9') {
++digit;
}

// check if the character is an empty space


else if (line[i] == ' ') {
++space;
}
}

printf("Vowels: %d", vowels);


printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);

return 0;
}

// Find the Frequency of a Character

#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

printf("Enter a character to find its frequency: ");


scanf("%c", &ch);

for (int i = 0; str[i] != '\0'; ++i) {


if (ch == str[i])
++count;
}

printf("Frequency of %c = %d", ch, count);


return 0;
}

You might also like