Write islower function

I have a homework question that asks:

Write a C++ function that has an input of a char value and returns true if the character is lower case or false otherwise.

I am having problem with this, cananyone guide me.
the following is what I have, I guess I'm confused.

#include <iostream>
#include <cmath>
#include <cctype>
using namespace std;

int checkCase(char x)
{
if
cout << islower (x);
return x;
}

int main ()

{

char ch;

cout << "Enter a character to see if it is lower case:" << endl;
cin >> ch;
checkCase (ch);
cout << "Character "<< ch << " is lower case." <<endl;



//The next two lines stop the Command Prompt window from closing
//until the user presses a key, including Enter.
//cout << "\n\nPress any key to exit." << endl;


return 0;
}
As I have understood you should write a function that returns true if a character is lower. So the function could look the following way

1
2
3
4
inline bool checkCase( char c )
{
   return ( islower( c ) );
}


Also maybe according to your assignment you should not use the standard function islower but you should write the same function youself.
Here's what I would do:
1
2
3
4
bool checkCase(char x)
{
    return (x >= 98 && x <= 123);
}

I hope that helps =D
Last edited on
SuperSonic,

As far as I know latin symbol 'a' has code 97 and latin symbol 'z' has code 122. Am I wrong?
> latin symbol 'a' has code 97 and latin symbol 'z' has code 122. Am I wrong?

On some (widely used) implementations that assumption is horribly wrong.
See: http://en.wikipedia.org/wiki/EBCDIC

This would be a lot better:
1
2
3
4
5
6
7
bool is_lower( char ch )
{
    static const char[] lcase_chars = "abcdefghijklmnopqrstuvwxyz" ;
    for( int i=0 ; i < (sizeof(lcase_chars)-1) ; ++i ) 
          if( ch == lcase_chars[i] ) return true ;
    return false ;
}

Thanks Every one,
this is what I came up with:
1
2
3
4
5
6
7
8
bool checkLowerCase (char x)
{
	if (x >= 'a' && x <= 'z')
	{
		return true;
	}
	else return false;
}
It is simply to write



1
2
3
4
inline bool checkLowerCase (char x)
{
	return ( x >= 'a' && x <= 'z' );
} 

Topic archived. No new replies allowed.