The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
to check variable if its non numeric sachin.gangadha Shell Programming and Scripting 3 12-06-2007 05:33 PM
Check for numeric inputs Raynon Shell Programming and Scripting 6 08-22-2007 03:17 AM
How to check for a valid numeric input Vijayakumarpc Shell Programming and Scripting 1 08-04-2007 08:34 AM
How to check a column contain numeric or char data type ?? jambesh Shell Programming and Scripting 12 10-06-2006 10:37 AM
Convert string to numeric kflee2000 Shell Programming and Scripting 3 11-19-2003 11:21 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-18-2007
knowledge_gain knowledge_gain is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 6
check the given string is numeric or not.

Hi,

how to check the given string is numeric or not , without converting ( using strtol...).

for ex: if string is C01 - non-numeric data
if string is 001 - numeric data

TIA
  #2 (permalink)  
Old 06-18-2007
ilko_partizan's Avatar
ilko_partizan ilko_partizan is offline
Registered User
  
 

Join Date: Apr 2007
Location: EU, Bulgaria
Posts: 33
You are trying with following standard C library function:

This function test only for decimal-digit character
http://www.freebsd.org/cgi/man.cgi?q...SD+6.2-RELEASE

This function test for hexadecimal-digit character
http://www.freebsd.org/cgi/man.cgi?q...SE&format=html

Best regards,
Iliyan Varshilov

Last edited by ilko_partizan; 06-18-2007 at 08:24 AM..
  #3 (permalink)  
Old 06-18-2007
knowledge_gain knowledge_gain is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 6
No.

i don't want to check in a loop using isdigit. need something simple.
  #4 (permalink)  
Old 06-18-2007
ilko_partizan's Avatar
ilko_partizan ilko_partizan is offline
Registered User
  
 

Join Date: Apr 2007
Location: EU, Bulgaria
Posts: 33
You are trying to solve your validation problem with regular expression library function .
http://www.opengroup.org/onlinepubs/...s/regcomp.html

Best regards,
Iliyan Varshilov

Last edited by ilko_partizan; 06-18-2007 at 08:46 AM..
  #5 (permalink)  
Old 06-18-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by knowledge_gain View Post
i don't want to check in a loop using isdigit. need something simple.
You are posting is a C programming forum and loops are too difficult?
  #6 (permalink)  
Old 06-18-2007
Octal Octal is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 67
Quote:
Originally Posted by knowledge_gain View Post
i don't want to check in a loop using isdigit. need something simple.
You could make your own function:
Code:
#include <stdlib.h>

short strchk (char *s) {
	unsigned short r = 0;

	while (*++s) {
		if (isdigit(s)) {
			r = 1;
		} else {
			r = 0;
			break;
		}
	}
	return r;
}
Note that I didn't compile this code, but it should work.
  #7 (permalink)  
Old 06-18-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
That routine does not check the first character of the string...

Try

Code:
int is_numeric(const char *p) {
     if (*p) {
          char c;
          while ((c=*p++)) {
                if (!isdigit(c)) return 0;
          }
          return 1;
      }
      return 0;
}
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 02:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0