views:

83

answers:

3

Hello,

Can anyone briefly explain how do these two commands work ?

isdigit and isalpha .. Ofcourse I read online sources before asking the question, but unfortunately I tried them and didnt get them to work.

What is the simplest way ?

I know it gives back a value, so im assuming I can use it like this :

if(isdigit(someinput)==1)
 return -1;

Is that correct ? can I use this for any number ? Can I compare it with a float number ? Can I compare arrays ?

Can I also use it when scanning documents or files ?

For example, if I want to scanf a text file that has numbers and letters, and determine is what im scanning (isdigit) or (isalpha) ??

Thanks :)

+1  A: 

They are not "commands", they are functions. Functions accept arguments and return values.

#include <ctype.h>
int isdigit( int ch );

This is the signature for the isdigit function: it indicates that it will accept an int value (or something that can be cast to int, like a char), and will return an int. You cannot, therefore, pass it an array (though you can call it on every member of an int[]).

The signature for isalpha is identical (except for the name, obviously).

The documentation says the following:

Description: The function isalpha() returns non-zero if its argument is a letter of the alphabet. Otherwise, zero is returned.

This means your comparison will not be correct for all implementations. Better to do something like:

if (isdigit(someinput)) {
 return -1;
}

In C, 0 will evaluate to false in a boolean expression, and all non-zero values evaluate to true. So this check will cover implementations of isdigit that return -1, 5, whatever.

If you want to apply these to values in a text file, you must read the text one character at a time and pass the characters you receive to those methods.

danben
Thanks ! So I can only use this to compare a single character ?
ZaZu
One invocation of this function will take a single character, but of course you can repeat as necessary.
danben
Thanks for replying, one final offtopic question...What is the method to convert a character to a number ?
ZaZu
If you want to get the ascii code of a `char`, just cast it to an `int`. If you want to convert from the character `'5'` to the integer value 5, you can do `'5' - '0'` (this will subtract the ascii value of `'0'` from that of `'5'`; the difference will be `5`).
danben
Thanks for the info :)
ZaZu
+1  A: 

You can use it for any char type! (actually it's int, I don't know whether this is for compatibilty reasons or whatnot, but the most common usage is for characters)

It will determine whether for example '5' is a digit or an alphanumerical (part of the alphabet). And yes, your usage is correct if someInput is of type "char"

LukeN
Thanks for replying :)
ZaZu
Hey, no problem! That's what I'm here for!
LukeN
+1  A: 

You do not normally want to compare the return value, just treat it as a Boolean:

if (isalpha(someinput))
    do_whatever();

If you insist on doing a comparison, it needs to be !=0, but it's entirely redundant and pointless.

You use it on characters that have been read from input, which are not (at that point) floats or anything, just groups of characters. In the case of a float, some of those will be digits, but many will also include decimal points (which aren't digits) and possibly an occasional e, + or - as well.

Also note that you normally need to cast the input to unsigned char before calling any of the is* functions. Many character sets will treat some characters as negative when they're viewed as signed char, and passing a negative value to any of these gives undefined behavior.

Jerry Coffin
What if the scanned input is a number that will be used later on for calculation ?
ZaZu
@ZaZu: if you've already converted something to a number, there's no longer any real use for `isdigit` or `isalpha` -- it's now a number, not a digit or an alphabetic character.
Jerry Coffin
How do you convert a char to a number ?
ZaZu
If the char is a digit, then you just subtract '0', ie: int num = ch - '0';
progrmr
Thanks for your help !
ZaZu