VB icon

Count chars

Email
Submitted on: 9/20/2018 11:07:26 AM
By: Thomas1965  
Level: Beginner
User Rating: Unrated
Compatibility: C
Views: 526
 
     Count vowels, consonants and whitespaces. Written in plain old C
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Count chars
// Description:Count vowels, consonants and whitespaces.
Written in plain old C
// By: Thomas1965
//**************************************

#ifdef _MSC_VER
 #pragma warning(disable: 4127)/*conditional expression is constant */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#define LINE_LENGTH 80
#define FALSE 0
#define TRUE !FALSE
#define EQUAL 0
int is_vowel(char ch);
int is_consonant(char ch);
char* get_line(char *buffer, int size);
void process_line(char* buffer, int* nvowels, int* nconsonants,int* nwhitespace); 
void print_results(int nvowels, int nconsonants, int nwhitespace);
int main()
{
 char buffer[LINE_LENGTH+1] = {0};
 int nvowels, nconsonants, nwhitespace;
 while(TRUE)
 {
nvowels = nconsonants = nwhitespace = 0;
printf("Enter some text - quit to exit app.\n");
get_line(buffer, LINE_LENGTH);
if (strcmp(buffer, "quit") == EQUAL)
 break; 
process_line(buffer, &nvowels, &nconsonants, &nwhitespace);
print_results(nvowels, nconsonants, nwhitespace);
 }
 return EXIT_SUCCESS;
}
void process_line(char* buffer, int* nvowels, int* nconsonants,int* nwhitespace)
{
 int i;
 assert(buffer != NULL);
 assert(nvowels != NULL && nconsonants != NULL && nwhitespace != NULL);
 
 for (i = 0; buffer[i] != '\0'; i++)
 {
if (is_vowel(buffer[i]))
 *nvowels += 1;
else if (is_consonant(buffer[i]))
 *nconsonants +=1;
else if (isspace(buffer[i]))
 *nwhitespace +=1;
 }
}
void print_results(int nvowels, int nconsonants, int nwhitespace)
{
 printf("Vowels: %d\n", nvowels); 
 printf("Consonants: %d\n", nconsonants); 
 printf("White space: %d\n", nwhitespace); 
}
/*
 Behaves like fgets but removes trailing '\n'
*/
char* get_line(char *buffer, int size)
{
 char *pos;
 
 assert(buffer != NULL);
 assert (size > 0);
 fgets(buffer, size, stdin);
 pos = strchr(buffer, '\n');
 if (pos)
*pos = '\0';
 return buffer;
}
int is_vowel(char ch)
{
 static const char *vowels = "aeiou";
 return strchr(vowels, tolower(ch)) != NULL;
}
int is_consonant(char ch)
{
 static const char *consonants = "bcdfghjklmnpqrstvwxyz";
 return strchr(consonants, tolower(ch)) != NULL;
}


Other 25 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.