Search This Blog

Wednesday, 5 December 2012

Convert lowercase letters to uppercase letters

Header file: cctype

// Converting lowercase letters to uppercase letters
#include <iostream>
#include <cctype> // For the use of toupper Function.
using namespace std;
int main()
{
 char a = 'a';
 char b = toupper(a);
 cout << b << endl;

 return 0;
}
//====================================

Better way:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
 char a = 'a';
 cout << static_cast<char>( toupper(a) ) << endl;
 return 0;
}
//---------------------------------------------------
//You will be thinking now, why we used static_cast<char> ?
//If we would have done as follows, what was wrong?

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
 char a = 'a';
 cout << toupper(a) << endl;
 return 0;
}

//Problem is that it'll return ASCII of  capital 'A', and we won't get capital A but ASCII of 'A'.

No comments:

Post a Comment