LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2005, 07:12 PM   #1
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
C equivalent of Java's isnumeric


Hi all,

I am wondering if C has an equivalent of Java's isNumeric method. I want users to be able to enter a range of IP addresses, like 10.1.1.5-15. I use sscanf( ip, "%d.%d.%d.%d", octet1, octet2, octet3, octet4 ) at the present to parse the octets into separate ints. However, I would like to allow the users to enter a range, and would use something like:
Code:
sscanf( ip, "%d.%d.%d.%s", octet1, octet2, octet3, strOctet4 );
if( isnumeric( strOctet4 )) {
    sscanf( strOctet4, "%d", octet4 );
}
else {
    /* use something like strtok() to break
        strOctet4 into two ints representing a
        lower and upper range.
     */
}
If there is no C function similar to isNumeric, what would you recommend? Checking the ASCII value of each char in strOctet4 to see if it falls in the range of a number?

Once again, thanks in advance!
 
Old 02-08-2005, 07:19 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
ctype.h has the following functions:

int isalnum(int c);
int isalpha(int c);
int isascii(int c);
int isblank(int c);
int iscntrl(int c);
int isdigit(int c);
int isgraph(int c);
int islower(int c);
int isprint(int c);
int ispunct(int c);
int isspace(int c);
int isupper(int c);
int isxdigit(int c);
 
Old 02-08-2005, 07:36 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Like jtshaw pointed out, ctype.h is where you're going to want to look. You can write you own isnumeric() like this:
Code:
int isnumeric(char *str)
{
  while(*str)
  {
    if(!isdigit(*str))
      return 0;
    str++;
  }

  return 1;
}
 
Old 02-08-2005, 08:06 PM   #4
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Brilliant--thanks, guys!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A port of Java's Properties in C++ xowl Programming 1 07-26-2005 07:45 AM
what is java's corresponding method for c's getch() bastin_gh Programming 3 07-19-2005 02:03 AM
visual c++ equivalent? tardigrade Linux - Software 2 10-18-2004 01:45 AM
java's sockets or python's friendlier to use? Dominik Programming 2 03-06-2004 06:19 AM
equivalent to c? codename000 Programming 3 04-03-2003 11:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration