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 : B. M. Harwani
2.5 (2)
close
close
Practical C Programming

Practical C Programming

2.5 (2)
By: B. M. 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

Converting the vowels in a sentence to uppercase

In this recipe, you will learn how to convert all of the lowercase vowels in a sentence to uppercase. The remaining characters in the sentence, including consonants, numbers, special symbols, and special characters, are simply ignored and will be left as they are.

Converting the casing of any letter is done by simply changing the ASCII value of that character, using the following formulas:

  • Subtract 32 from the ASCII value of a lowercase character to convert it to uppercase
  • Add 32 to the ASCII value of an uppercase character to convert it to lowercase

The following diagram shows the ASCII values of the uppercase and lowercase vowels:

Figure 2.12

The ASCII value of the uppercase letters is lower than that of lowercase letters, and the difference between the values is 32.

How to do it…

  1. Create a string called str to input your sentence. As usual, the last character will be a null character:
char str[255];
  1. 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, and initialize the i variable to 0, since each character of the sentence will be accessed through i:
gets(str);
i=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:
while(str[i]!='\0')
{
{ …
}
}
i++;
  1. Check each letter to verify whether it is a lowercase vowel. If the accessed character is a lowercase vowel, then the value 32 is subtracted from the ASCII value of that vowel to convert it to uppercase:
if(str[i]=='a' ||str[i]=='e' ||str[i]=='i' ||str[i]=='o' ||str[i]=='u')
str[i]=str[i]-32;
  1. When all of the letters of the sentence have been accessed, then simply display the entire sentence.

The convertvowels.c program for converting the lowercase vowels in a sentence to uppercase is as follows:

#include <stdio.h>
void main()
{
char str[255];
int i;
printf("Enter a sentence: ");
gets(str);
i=0;
while(str[i]!='\0')
{
if(str[i]=='a' ||str[i]=='e' ||str[i]=='i' ||str[i]=='o'
||str[i]=='u')
str [i] = str [i] -32;
i++;
}
printf("The sentence after converting vowels into uppercase
is:\n");
puts(str);
}

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

How it works...

Again, we will assume that you will not enter a sentence longer than 255 characters. Therefore, we have defined our string array, str , to be of the size 255. When prompted, enter a sentence to assign to the str array. Because a sentence may have blank spaces between the words, instead of scanf, we will use the gets function to accept the sentence.

To access each character of the sentence, we will execute a while loop that will run until the null character is reached in the sentence. After each character of the sentence, it is checked whether it is a lowercase vowel. If it is not a lowercase vowel, the character is ignored and the next character in the sentence is picked up for comparison.

If the character that's accessed is a lowercase vowel, then a value of 32 is subtracted from the ASCII value of the character to convert it to uppercase. Remember that the difference in the ASCII values of lowercase and uppercase letters is 32. That is, the ASCII value of lowercase a is 97 and that of uppercase A is 65. So, if you subtract 32 from 97, which is the ASCII value of lowercase a, the new ASCII value will become 65, which is the ASCII value of uppercase A.

The procedure of converting a lowercase vowel to an uppercase vowel is to first find the vowel in a sentence by using an if statement, and then subtract the value 32 from its ASCII value to convert it to uppercase.

Once all of the characters of the string are accessed and all of the lowercase vowels of the sentence are converted to uppercase, the entire sentence is displayed using the puts function.

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

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

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

D:\CBook>./convertvowels
Enter a sentence: It is very hot today. Appears as if it might rain. I like rain
The sentence after converting vowels into uppercase is:
It Is vEry hOt tOdAy. AppEArs As If It mIght rAIn. I lIkE rAIn

Voilà! We've successfully converted the lowercase vowels in a sentence to uppercase.

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