StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Program #28 (U3): Count Character Categories in a String
Download
  1. C Programming
  2. C Programming Practice
Program #27 (U3): String Utilities without <string.h> : Program #29 (U3): Count Words in a Sentence
C Programming Practice

Task: Read a line and count vowels, consonants, digits, spaces, and special characters. Print category-wise totals.

 
#include <stdio.h>

#include <ctype.h>

 
int isvowel(int ch) {

    ch = tolower(ch);

    return (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u');

}

 
int main(void) {

    char s[512];

    int v=0, cons=0, dig=0, sp=0, spec=0, c;

 
    printf("Enter a line: ");

    int i=0; while ((c=getchar())!='\n' && c!=EOF) { if(i<511) s[i++]=(char)c; } s[i]='\0';

 
    for (i=0; s[i]; ++i) {

        if (isalpha((unsigned char)s[i])) {

            if (isvowel(s[i])) ++v; else ++cons;

        } else if (isdigit((unsigned char)s[i])) {

            ++dig;

        } else if (isspace((unsigned char)s[i])) {

            ++sp;

        } else {

            ++spec;

        }

    }

 
    printf("Vowels=%d Consonants=%d Digits=%d Spaces=%d Special=%d\n", v, cons, dig, sp, spec);

    return 0;

}

 

 

Program #27 (U3): String Utilities without <string.h> Program #29 (U3): Count Words in a Sentence
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website