User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 374,185 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,446 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 253 | Replies: 5
Reply
Join Date: Apr 2008
Posts: 2
Reputation: info04 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
info04 info04 is offline Offline
Newbie Poster

function problem

  #1  
May 9th, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 151
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 16
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: function problem

  #2  
May 9th, 2008
Why u are converting a char to int, you can compare char same as u compare int.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,588
Reputation: iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough 
Rep Power: 15
Solved Threads: 293
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: function problem

  #3  
May 9th, 2008
How about...

  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Foo
  7. {
  8. public:
  9. bool isUpperCase( char t )
  10. {
  11. string crap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  12. int size = crap.length();
  13.  
  14. // check if the same return true
  15. //otherwise return false
  16.  
  17. };
  18.  
  19.  
  20. int main()
  21. {
  22. Foo test;
  23. cout << test.isUpperCase('a') << endl;
  24.  
  25. cout << test.isUpperCase('A');
  26.  
  27. cin.get();
  28. }
Last edited by iamthwee : May 9th, 2008 at 5:25 am.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Nov 2007
Posts: 770
Reputation: mitrmkar is a jewel in the rough mitrmkar is a jewel in the rough mitrmkar is a jewel in the rough 
Rep Power: 4
Solved Threads: 154
mitrmkar mitrmkar is offline Offline
Master Poster

Re: function problem

  #4  
May 9th, 2008
Originally Posted by info04 View Post
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/
Reply With Quote  
Join Date: Apr 2008
Posts: 275
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 37
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: function problem

  #5  
May 9th, 2008
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).

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

krs,
tesu
Reply With Quote  
Join Date: Mar 2008
Location: UK
Posts: 239
Reputation: williamhemswort has a spectacular aura about williamhemswort has a spectacular aura about 
Rep Power: 2
Solved Threads: 31
williamhemswort's Avatar
williamhemswort williamhemswort is offline Offline
Posting Whiz in Training

Re: function problem

  #6  
May 9th, 2008
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'.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:48 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC