A simple program that I wrote using C programming language to ask the user to give a string and then the program will count the number of vowels, and consonants improved version and display the results on the screen.

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 at my email address also. Thank you.

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

My mobile number here in the Philippines is 09173084360.

Program Listing

/* main.c
  Prof. Jake Rodriguez Pomperada, MAED-IT, MIT
  www.jakerpomperada.blogspot.com and www.jakerpomperada.com
  jakerpomperada@gmail.com
  Bacolod City, Negros Occidental Philippines
  August 19, 2021   Thursday   6:26 AM
*/

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

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

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

bool is_consonant(char ch)
{
  const char consonats[] = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";

  return strchr(consonats, ch) != NULL;
}

int main()
{
  char str[100]; int i=0;
  int vowels=0,consonants=0, a = 0;

  printf("\n\n\tConsonants and Vowels in C Improved Version\n");
  printf("\nGive a String : ");
  // scanf("%s",&str); reads only till the first whitespace
  fgets(str, sizeof(str), stdin);
  char *pos = strrchr(str, '\n'); 
  // fgets sometines leaves a trailing \n in the str
  if(pos != NULL)
    pos = '\0';
  for(i=0; str[i]!='\0'; i++)
  {
    if(is_vowel(str[i]))
    {
      a++;
      vowels++;
      printf("\n %d. The Vowels is: %c",a,str[i]);
    }
    else if (is_consonant(str[i]))
    {
      a++;
      consonants++;
      printf("\n %d. The Consonant is: %c",a,str[i]);
    }
  }
  printf("\n\n\nThe Total Number of Vowels : %d",vowels);
  printf("\nThe Total Number of Consonants  : %d",consonants);
  printf("\n\n\tEnd of Program\n\n");

  return EXIT_SUCCESS;
} /* End of Code */

Leave a Reply

Your email address will not be published.