PART B · PRESCRIBED EXERCISE 07
Count character categories in a string
Understand the algorithm. Run the code.
Explain the result with confidence.
LAB B07 · 01
Understand the task.
Aim: Count character categories in a string.
Character classification. Character classification places input characters into defined categories. Here alphabets are also divided into vowels and consonants; digits, literal spaces and remaining characters form the other primary categories.
Review the related theory in Unit 2.
LAB B07 · 02
Know what goes in and what comes out.
One line of at most 199 characters. Vowels are English a, e, i, o, u in either case. Spaces means the literal space character; tabs and punctuation count as special characters. The input newline is excluded.
Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.
LAB B07 · 03
Plan the steps before writing code.
- Read a bounded line, find its length and remove the newline.
- Initialize all six counters to zero.
- For each character, convert its value to unsigned char before passing it to ctype functions.
- If alphabetic, increment alphabets and then either vowels or consonants.
- Otherwise increment digits, literal spaces, or special characters according to the matching category.
- Display all counters.
LAB B07 · 04
One complete program. Ready to run.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char text[201];
int length = 0, i, lower;
int alphabets = 0, digits = 0, vowels = 0, consonants = 0, spaces = 0, special = 0;
unsigned char ch;
if (fgets(text, sizeof text, stdin) == NULL) { puts("Enter a line of text."); return 1; }
while (text[length] != '\0' && text[length] != '\n') length++;
if (length > 199) { puts("Use at most 199 characters."); return 1; }
text[length] = '\0';
for (i = 0; i < length; i++) {
ch = (unsigned char)text[i];
if (isalpha(ch)) {
alphabets++;
lower = tolower(ch);
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') vowels++;
else consonants++;
} else if (isdigit(ch)) digits++;
else if (ch == ' ') spaces++;
else special++;
}
printf("Alphabets = %d\nDigits = %d\nVowels = %d\nConsonants = %d\nSpaces = %d\nSpecial characters = %d\n",
alphabets, digits, vowels, consonants, spaces, special);
return 0;
}
Alphabets = 6
Digits = 3
Vowels = 2
Consonants = 4
Spaces = 2
Special characters = 1
Compile on your own computer
gcc B07-character-counts.c -o programRun the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.
LAB B07 · 05
Connect each statement to its purpose.
- isalpha and isdigit determine the primary alphabet and digit categories. The program uses the default C locale.
- tolower lets the vowel test use one lowercase set for both uppercase and lowercase letters.
- The unsigned-char conversion satisfies the ctype argument rule even on implementations with signed char.
- Vowels and consonants are subsets of alphabets: vowels + consonants = alphabets. Do not count alphabets again when totaling the primary categories.
- Alphabets + digits + spaces + special equals the processed line length. This is single-byte classification, not Unicode text analysis.
LAB B07 · 06
Follow the sample calculation.
| Step or component | What happens |
|---|---|
| BCA Lab | 6 alphabetic characters: A and a are 2 vowels, leaving 4 consonants. |
| 123 | 3 digits |
| Separators | 2 literal spaces |
| ! | 1 special character |
| Cross-check | 6 + 3 + 2 + 1 = 12 processed characters |
LAB B07 · 07
Check more than one input.
| Try this input or case | Expected behaviour |
|---|---|
| AEIOU xyz | 8 alphabets, 5 vowels, 3 consonants, 1 space. |
| Empty line | All counters are zero. |
| A tab between two digits | 2 digits, 0 spaces, 1 special character. |
Time and storage
O(L) time for line length L, with fixed buffer capacity and O(1) counter space.
Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.
LAB B07 · 08
Use the real C compiler.
Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.
Load the compiler when you’re ready to practise.
Your C workspace
Edit real C code and see real compiler output.
Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.
View / copy the selected source
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char text[201];
int length = 0, i, lower;
int alphabets = 0, digits = 0, vowels = 0, consonants = 0, spaces = 0, special = 0;
unsigned char ch;
if (fgets(text, sizeof text, stdin) == NULL) { puts("Enter a line of text."); return 1; }
while (text[length] != '\0' && text[length] != '\n') length++;
if (length > 199) { puts("Use at most 199 characters."); return 1; }
text[length] = '\0';
for (i = 0; i < length; i++) {
ch = (unsigned char)text[i];
if (isalpha(ch)) {
alphabets++;
lower = tolower(ch);
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') vowels++;
else consonants++;
} else if (isdigit(ch)) digits++;
else if (ch == ' ') spaces++;
else special++;
}
printf("Alphabets = %d\nDigits = %d\nVowels = %d\nConsonants = %d\nSpaces = %d\nSpecial characters = %d\n",
alphabets, digits, vowels, consonants, spaces, special);
return 0;
}
LAB B07 · 09
Explain why the program works.
Why not total all six counters?
Vowels and consonants are already included in alphabets; adding all six double-counts letters.
Why use unsigned char?
ctype functions require an argument representable as unsigned char, or EOF. A negative signed-char value is not valid.
Does y count as a vowel here?
No. This explicitly stated practice definition uses only a, e, i, o and u.
LAB B07 · 10
Build the next skill.
Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.