Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-15-2006, 02:57 PM   #1
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
c: find out if a string represents a number?

i have a char array, which contains the "names" of the entries of the /proc directory. I need to get the process identifiers. How can i find out if the "name" of an entry is a number, ie a process id? I don't think, that casting it to int would solve the problem since a string would turn to a number, too. Is there a better way?

Old 08-15-2006, 03:32 PM   #2
Registered: Apr 2006
Location: London, Uk
Distribution: Ubuntu on Desktop
Posts: 121

Rep: Reputation: 15
You probably wont be able to use atoi for this because that will simply return 0.
However, you could use strtol, and check the returned endpointer. Try "man strtol" to see how you might do this. pay attention to what happens to the endptr parameter.

Old 08-15-2006, 03:57 PM   #3
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
you could just go through each char in the string and check the value of the char, as long as every chars dec value is between 48 and 57 you know you have a number, then call atoi on it.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trying to figure out the number of characters in a std::string, using length. RHLinuxGUY Programming 4 05-23-2006 10:39 AM
Counting number of occurences of a certain chacter in string java linuxmandrake Programming 4 04-28-2006 12:10 AM
Getting a number out of a string in bash Luc484 Programming 5 01-20-2006 03:15 AM
strip number from string (c code) alaios Programming 8 09-26-2005 01:47 AM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 10:33 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:33 AM.

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