Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

LordG

Numeric String in C

This topic is 5864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Yes, I believe so.


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

The isdigit function tests for any decimal-digit character , i.e. characters between 0 and 9 inclusive.


Although this might not be part of the standard.

Share this post


Link to post
Share on other sites
quote:
Original post by LordG
Does C have any function that determines whether a string is numeric or not?


indirectly, yes. strtol

you, can check the stop character and see if it points to end of the string?


but, if you want the code.

    
bool is_string_decimal(const string* str)
{
bool result = true;
size_t i;

for (i = 0; i < strlen(str); ++i) {
if (isdigit(str) == 0) {
i = (strlen - 1);
result = false;
}
}
}


To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

[edited by - jenova on June 29, 2002 1:46:44 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by LordG
Can you tell me what does that function do?

What is it suppose to return?


int isdigit(int c);

Individual chars are often represented as ints in some of the C functions. The function tests a particular char to determine if it represents a digit character (0-9) and returns an int value as a boolean (true == 1, false == 0). In C there is no boolean type (well there is in C99 but anyway...). I think this is an older function. [edit: it's a very old function. It's defined in K & R!]

int isDigit = isdigit(stringarray[N]);

I suspect that isdigit operates by subtracting 48 from the argument and checking that result to see if it falls in the range 0 - 9. The ascii/ansi code for 0 is 48 (0x30), 1 is 49 (0x31) and so on upto 9 which is 57 (0x39).

//edit: it's actually a macro in some implementations.
#define isdigit(_c) ( _pctype[_c] & _DIGIT )
where
#define _DIGIT 0x4 /* digit[0-9] */
and
_CRTIMP extern unsigned short *_pctype;

_pctype is a pointer to the _ctype table defined in ctype.c which is kind of interesting to examine if you have the crtdll sources.


[edited by - lessbread on June 29, 2002 2:18:04 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!