Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Practical C Programming
  • Table Of Contents Toc
Practical C Programming

Practical C Programming

By : Harwani
2.5 (2)
close
close
Practical C Programming

Practical C Programming

2.5 (2)
By: Harwani

Overview of this book

Used in everything from microcontrollers to operating systems, C is a popular programming language among developers because of its flexibility and versatility. This book helps you get hands-on with various tasks, covering the fundamental as well as complex C programming concepts that are essential for making real-life applications. You’ll start with recipes for arrays, strings, user-defined functions, and pre-processing directives. Once you’re familiar with the basic features, you’ll gradually move on to learning pointers, file handling, concurrency, networking, and inter-process communication (IPC). The book then illustrates how to carry out searching and arrange data using different sorting techniques, before demonstrating the implementation of data structures such as stacks and queues. Later, you’ll learn interesting programming features such as using graphics for drawing and animation, and the application of general-purpose utilities. Finally, the book will take you through advanced concepts such as low-level programming, embedded software, IoT, and security in coding, as well as techniques for improving code performance. By the end of this book, you'll have a clear understanding of C programming, and have the skills you need to develop robust apps.
Table of Contents (20 chapters)
close
close

Counting vowels and consonants in a sentence

In this recipe, you will learn how to count the number of vowels and consonants in an entered sentence. The vowels are a, e, i, o, and u, and the rest of the letters are consonants. We will use ASCII values to identify the letters and their casing:

Figure 2.11

The blank spaces, numbers, special characters, and symbols will simply be ignored. 

How to do it…

  1. Create a string array called str to input your sentence. As usual, the last character will be a null character:
char str[255];
  1. Define two variables, ctrV and ctrC:
int  ctrV,ctrC;
  1. Prompt the user to enter a sentence of your choice:
printf("Enter a sentence: ");
  1. Execute the gets function to accept the sentence with blank spaces between the words:
gets(str);

  1. Initialize ctrV and ctrC to 0. The ctrV variable will count the number of vowels in the sentence, while the ctrC variable will count the number of consonants in the sentence:
ctrV=ctrC=0;
  1. Execute a while loop to access each letter of the sentence one, by one until the null character in the sentence is reached.
  2. Execute an if block to check whether the letters are uppercase or lowercase, using ASCII values. This also confirms that the accessed character is not a white space, special character or symbol, or number. 
  3. Once that's done, execute a nested if block to check whether the letter is a lowercase or uppercase vowel, and wait for the while loop to terminate:
while(str[i]!='\0')
{
if((str[i] >=65 && str[i]<=90) || (str[i] >=97 && str[i]<=122))
{
if(str[i]=='A' ||str[i]=='E' ||str[i]=='I' ||str[i]=='O'
||str[i]=='U' ||str[i]=='a' ||str[i]=='e' ||str[i]=='i'
||str[i]=='o'||str[i]=='u')
ctrV++;
else
ctrC++;
}
i++;
}

The countvowelsandcons.c program for counting vowels and consonants in a string is as follows:

#include <stdio.h>
void main()
{
char str[255];
int ctrV,ctrC,i;
printf("Enter a sentence: ");
gets(str);
ctrV=ctrC=i=0;
while(str[i]!='\0')
{
if((str[i] >=65 && str[i]<=90) || (str[i] >=97 && str[i]<=122))
{
if(str[i]=='A' ||str[i]=='E' ||str[i]=='I' ||str[i]=='O'
||str[i]=='U' ||str[i]=='a' ||str[i]=='e' ||str[i]=='i'
||str[i]=='o'||str[i]=='u')
ctrV++;
else
ctrC++;
}
i++;
}
printf("Number of vowels are : %d\nNumber of consonants are :
%d\n",ctrV,ctrC);
}

Now, let's go behind the scenes to understand the code better.

How it works...

We are assuming that you will not enter a sentence longer than 255 characters, so we have defined our string variable accordingly. When prompted, enter a sentence that will be assigned to the str variable. Because a sentence may have blank spaces between the words, we will execute the gets function to accept the sentence. 

The two variables that we've defined, that is, ctrV and ctrC, are initialized to 0. Because the last character in a string is always a null character, \0, a while loop is executed, which will access each character of the sentence one by one until the null character in the sentence is reached.

Every accessed letter from the sentence is checked to confirm that it is either an uppercase or lowercase character. That is, their ASCII values are compared, and if the ASCII value of the accessed character is a lowercase or uppercase character, only then it will execute the nested if block. Otherwise, the next character from the sentence will be accessed.

Once you have ensured that the accessed character is not a blank space, any special character or symbol, or a numerical value, then an if block will be executed, which checks whether the accessed character is a lowercase or uppercase vowel. If the accessed character is a vowel, then the value of the ctrV variable is incremented by 1. If the accessed character is not a vowel, then it is confirmed that it is a consonant, and so the value of the ctrC variable is incremented by 1.

Once all of the characters of the sentence have been accessed, that is, when the null character of the sentence is reached, the while loop terminates and the number of vowels and consonants stored in the ctrV and ctrC variables is displayed on the screen.

Let's use GCC to compile the countvowelsandcons.c program, as follows:

D:\CBook>gcc countvowelsandcons.c -o countvowelsandcons

Let's run the generated executable file, countvowelsandcons.exe, to see the output of the program:

D:\CBook>./countvowelsandcons
Enter a sentence: Today it might rain. Its a hot weather. I do like rain
Number of vowels are : 18
Number of consonants are : 23

Voilà! We've successfully counted all of the vowels and consonants in our sentence.

Now, let's move on to the next recipe!

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Practical C Programming
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon