Categories
C

Count Vowels, Consonants, and Digits in C

 In this tutorial our program will count the number of vowels, consonants, and digits using C programming language. I hope you will find my  work useful.I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

Program Listing

/* vowels_consonants.c
   Author : Jake Rodriguez Pomperada, MAED-IT, MIT
   www.jakerpomperada.com   www.jakerpomperada.blogspot.com
   jakerpomperada@gmail.com
   Bacolod City, Negros Occidental Philippines
*/

#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>

typedef struct
{
	int vowel_count;
	int consonant_count;
	int digit_count;
}Stat;

bool is_vowel(char ch)
{
	const char vowels[] = "aeiou";

	return strchr(vowels, tolower(ch)) != NULL;
}

bool is_consonant(char ch)
{
	const char consonants[] = "bcdfghjklmnpqrstuvwxyz";

	return strchr(consonants, tolower(ch)) != NULL;
}

void do_stats(const char* txt, size_t size, Stat *stat)
{
	assert(txt != NULL && size > 1);
	assert(stat != NULL);

	const char *cp = txt;

	while (size--)
	{
		if (is_vowel(*cp))
			stat->vowel_count++;
		else if (is_consonant(*cp))
			stat->consonant_count++;
		else if (isdigit(*cp))
			stat->digit_count++;
		cp++;
	}
}

void print_stats(const Stat* stat)
{
	assert(stat != NULL);

	printf("\tStatistics:\n");
	printf("\t===========\n");
	printf("\t  Vowels    : %d\n", stat->vowel_count-1);
	printf("\t  Consonants: %d\n", stat->consonant_count);
	printf("\t  Digits    : %d\n", stat->digit_count);
}

int main()
{
	Stat stat = {0,0,0};
	printf("\n\n");
	printf("\tCount Vowels,Consonants, and Digits in C");
	printf("\n\n");
	const char input[] = "Jake Pomperada, 42 years old and 173 cm tall.";
	printf("\t%s", input);
	printf("\n\n");
	do_stats(input, sizeof(input), &stat);
	print_stats(&stat);
	printf("\n\n");
	system("pause");
}
Categories
C

Learning Math in C

A menu driven program that I wrote using C language to solve the factorial, prime numbers, and odd and even numbers.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

Program Listing

#include<stdio.h>
#include <stdlib.h>

int main()

{
    printf("\n\n\t\tLearning Math in C\n");
    printf("\nCreated By Mr. Jake R. Pomperada,MAeD-IT,MIT\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);
        
        switch(choice)
        {
            case 1:
                printf("Enter number:\n");
                scanf("%d", &num);
                fact = 1;
                for(i = 1; i <= num; i++)
                {
                    fact = fact*i;
                }
                printf("\n\nFactorial value of %d is = %lu\n\n\n",num,fact);
                break;
        
            case 2:
                printf("Enter number:\n");
                scanf("%d", &num);
                if(num == 1)
                printf("\n1 is neither prime nor composite\n\n");
                for(i = 2; i < num; i++)
                {
                    if(num%i == 0)
                    {
                        printf("\n%d is not a prime number\n\n", num);
                        break;
                    }
                
                }
                /*
                    Not divisible by any number other 
                    than 1 and itself
                */
                if(i == num) 
                {
                    printf("\n\n%d is a Prime number\n\n", num);
                    break;
                }
        
            case 3:
                printf("Enter number:\n");
                scanf("%d", &num);
        
                if(num%2 == 0) // 0 is considered to be an even number
                    printf("\n\n%d is an Even number\n\n",num);
                else
                    printf("\n\n%d is an Odd number\n\n",num);
                break;
        
            case 4:
                exit (0);
                
            case 5: 
				break; 
        
      			default:
        		printf("\n\nINPUT CORRECT OPTION\n\n\n");
       			break; 
        }
    }
    return 0;
}