Thread: Getting part of string with sscanf()

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1,066

    Getting part of string with sscanf()

    I'm currently working through the book "C Programming: A Modern Approach" (by K.N.King) and I'm having some problems with the following exercise:
    Assume that "str" is a string that contains a "sales rank" immediately preceded by the # symbol (other characters may precede the # and/or follow the sales rank). A sales rank is a series of decimal digits possibly containing commas, such as the following examples:
    989
    24,675
    1,162,620
    Write a call of "sscanf" that extracts the sales rank (but not the # symbol) and stores it in a string variable named "sales_rank".
    My solution is
    Code:
    sscanf(str, "%*[^#]#%[0-9,]", sales_rank);
    which works except for the case where "str" starts with the # symbol (without any characters preceding it).
    After rereading the corresponding parts in the book and of the man page of sscanf I don't see any way to have a single call of sscanf so that it works both for any characters before # and no character before #.
    Am I missing something or misinterpreting the exercise?

    Bye, Andreas

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Good question. It doesn't seem possible since %*[^#] says to read at least one non # character. Can you do it with more than one sscanf?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Of course right after I say it can't be done I realize that it can. Consider a format string like this:
    Code:
    (*str=='#') ? "#%[0-9,]" : "%*[^#]#%[0-9,]"
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Thanks, that's probably the solution the author thought about.

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by oogabooga View Post
    Of course right after I say it can't be done I realize that it can. Consider a format string like this:
    Code:
    (*str=='#') ? "#%[0-9,]" : "%*[^#]#%[0-9,]"
    Or more succinctly:
    Code:
    "%*[^#]#%[0-9,]"+6*(*str=='#')
    But I recommend a solution like that only for the IOCCC.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf for a string from a file
    By biaspoint in forum C Programming
    Replies: 6
    Last Post: 09-06-2010, 08:35 PM
  2. sscanf()-getting integer from a string
    By saman_glorious in forum C Programming
    Replies: 8
    Last Post: 09-02-2010, 08:47 AM
  3. sscanf string match
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 01-25-2010, 10:27 PM
  4. Using sscanf to parse string
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 06:20 PM
  5. sscanf string parsing
    By penney in forum C Programming
    Replies: 5
    Last Post: 02-03-2003, 11:28 AM