Uppercase/lowercase and vice-versa conversion of alphabets
Using toupper() and tolower() function for character level conversion in its ascii integer form.
Learning Objectives
- Use of toupper() and tolower() function of the header ctype.h for single character level conversion.
Source Code
|
|
Run Output
Code Understanding
#include <ctype.h>
Use of this header is required to use the toupper() and tolower() functions.
char c=’a’; char d=’A’;
Alphabets declaration to be used for conversion.
cout<<“toupper(“<<c<<“) = “<<(char) toupper(c)<<endl;
cout<<“tolower(“<<d<<“) = “<<(char) tolower(d)<<endl;
Both converted alphabets are printed here. For output of toupper() and tolower() are in integer forms so they have to be casted as char type so that correct output is displayed.
int a=97; int b=65;
This are values to be tested in integer form where the integer ascii value represent an alphabet.
cout<<“toupper(“<<a<<“) = “<<toupper(c)<<endl;
cout<<“toupper(“<<b<<“) = “<<tolower(c)<<endl;
Here the pure integer forms are tested for conversion where input values are lower and upper alphabet ascii value. These values will be converted to integer values of upper and lower values respectively.
int p=34;
This number is to test a value which is not ascii code of an alphabet.
cout<<“toupper(“<<p<<“) = “<<toupper(p)<<endl;
cout<<“tolower(“<<p<<“) = “<<tolower(p)<<endl;
When the input values are not ascii code of an alphabet, the functions will return the same ascii code in integer form.
Common Errors
- Students must keep in mind that toupper() and tolower() functions take int input and return int output so suitable casting may be required to show it in alphabet form.
Suggested Filename(s): toupper-tolower.cpp
Created: 3-Jan-2018 20:23 | Updated: 3-Jan-2018 21:55|
