I am writing a function which will check if the letter is in upper case , if so it returns true otherwise false.
Following is the code, I have written, but what is wrong?

#include<iostream>


using namespace std;


using std::cout;
using std::cin;
using std::endl;


int isUpperCase ( char );


int main()


{
int i;
char c;


cout<<"Enter a charactor : ";
cin>>c;


i = int ( c );


isUpperCase ( i );



return 0;



}


int isUpperCase ( i )


{
if ( i > 65 && i < 90 )


return 1;


else


return 0;


}

please comment.

Recommended Answers

All 5 Replies

Why u are converting a char to int, you can compare char same as u compare int.

Member Avatar for iamthwee

How about...

#include <iostream>
#include <string>

using namespace std;

class Foo
{
public:
   bool isUpperCase( char t )
   {
     string crap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     int size = crap.length();
     
      // check if the same return true 
     //otherwise return false

};


int main()
{ 
  Foo test;
  cout << test.isUpperCase('a') << endl;
  
  cout << test.isUpperCase('A');
  
  cin.get();
}

but what is wrong?

You are excluding the lower and upper bounds, so you could change to if ( i >= 65 && i <= 90 ) or if ( i > 64 && i < 91 ) The standard libraries provide functions for checking characters, see e.g. here
http://www.cplusplus.com/reference/clibrary/cctype/

hi,
you can also mask bit on position# 6, which is 0 if char is upper case and 1 if char is lower case, e.g. 'A' = 0x41 = 0100 0001 (binary), 'a' = 0x61 = 0110 0001 (binary).

bool isUpperCase (char c) {return !(c&0x20);}
. . .
char c='z';
cout<<c<<(isUpperCase(c)?" is upper ":" is lower ")<<"char";
// z is lower char

krs,
tesu

I see nothing wrong with the function isUpperCase except from what mitrmkar sead, it would not work if you passed the function the characters 'A' or 'Z'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.