isString to recognize strings

This is a discussion on isString to recognize strings within the C Programming forums, part of the General Programming Boards category; Hi everyone, I have been looking for a sort of isstr function to recognize if a string read from a ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    isString to recognize strings

    Hi everyone,

    I have been looking for a sort of isstr function to recognize if a string read from a file is a string of characters or or a string of numbers, but apparently this function is not a standard C function.

    Does anyone have a similar function implemented?

    Thank you in advance,

    All the best,

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, if you don't really care about the contents you can try one of the functions in atoi,atol,afof family. They usually return 0 if the argument string cannot be converted to whatever type the function returns. So then you would know it's not a number.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi Claudio, thanks for the quick reply.

    That's an option. I was not sure whether those functions would return an integer if it surely cannot be converted; I in fact imagined they would return whatever binary sequence the string could be identified with.

    Thank you.
    Best
    cfd

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by cfdprogrammer View Post
    Hi Claudio, thanks for the quick reply.

    That's an option. I was not sure whether those functions would return an integer if it surely cannot be converted; I in fact imagined they would return whatever binary sequence the string could be identified with.

    Thank you.
    Best
    cfd
    Well as far as I know they return 0 on failure. However not sure what atoi("0") would return.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I guess using strtol() is actually better.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    I don't know how in-depth you're going to be with this, but you could always end up testing to see if the first character is an integer (==1, ==2, ..., ==0), then the second, and so on. If one of the slots in the array doesn't equal from 1 to 0 (or 0 to 9) (excluding terminating \0 or /0, i forget. It's possible that it's got only letters. I'm not sure how efficient it would be, but the code wouldn't take but a few minutes to write.

    Probably not the best idea in the world, but if you can't figure something out...this should work.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by claudiu View Post
    Well as far as I know they return 0 on failure. However not sure what atoi("0") would return.
    atoi("0") returns 0.
    you need to check errno to see if atoi() failed.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by cpjust View Post
    atoi("0") returns 0.
    you need to check errno to see if atoi() failed.
    I agree with claudiu that strotol is probably better... As you can't tell using atoi whether a string was "123sometext" or "123", afaik.
    The atoi manpage doesn't even mention errno being set by atoi.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by EVOEx View Post
    I agree with claudiu that strotol is probably better... As you can't tell using atoi whether a string was "123sometext" or "123", afaik.
    The atoi manpage doesn't even mention errno being set by atoi.
    You're right, I can't find any mention of errno in the Linux man pages, but I did find this:
    atoi(3)
    Quote Originally Posted by HP
    Since these functions return 0 (zero), INT_MIN, INT_MAX, LONG_MIN,
    LONG_MAX, and ULONG_MAX in the event of an error and these values are also
    valid returns if the function is successful, applications should set errno
    to 0 (zero) before calling these functions, and check errno after return
    from the function. If errno is nonzero, an error occurred.
    and this:
    'atoi' does not reset 'errno' in the case of no error | Microsoft Connect

    So maybe setting errno isn't actually in the standard for atoi(). I haven't used it for a hell of a long time. strtol() is definitely better though.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21