Thread: Probllem I'm Solving

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    22

    Probllem I'm Solving

    Hi there;

    I need a help with a problem I am trying to code around.

    I need to take in a line - in the form "xxx $y, $y, $y". The line is always in this form. xxx can be any character, and the following $y's are $anynumber - seperated by commas.

    The tokens may be seperated by no spaces, a single space, tabs, multiple spaces or a combination of both. There is always at least one spaxe seperating "xxx" from the first "$y", however.

    I need to parse these into four arrays, but not sure where to start with the space-handling.

    Any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The ever flexible sscanf....
    Code:
    $ cat foo.c && gcc foo.c && ./a.out
    #include <stdio.h>
    int main ( ) {
      char test[] = "abc $12, $34, $567";
      char string[10];
      int  x, y, z;
    
      if ( sscanf( test, "%s $%d, $%d, $%d",
                   string, &x, &y, &z ) == 4 ) {
        printf( "Conversion OK\n" );
        printf( "String=%s, numbers=%d,%d,%d\n", string, x, y, z );
      }
      return 0;
    }
    
    
    Conversion OK
    String=abc, numbers=12,34,567
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    I think you can use scanf because I think it sort of
    ignores white space (probably)



    scanf "%s $%d, $%d, $%d", strval, &yy, &xx, &zz);


    Some of the characters may be special so might need a \ before
    then such as

    %s \$%d, \$%d, \$%d",


    Less likely is
    "%s \$%d\, \$%d\, \$%d"

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    22
    Quote Originally Posted by Salem
    The ever flexible sscanf....
    Code:
    $ cat foo.c && gcc foo.c && ./a.out
    #include <stdio.h>
    int main ( ) {
      char test[] = "abc $12, $34, $567";
      char string[10];
      int  x, y, z;
    
      if ( sscanf( test, "%s $%d, $%d, $%d",
                   string, &x, &y, &z ) == 4 ) {
        printf( "Conversion OK\n" );
        printf( "String=%s, numbers=%d,%d,%d\n", string, x, y, z );
      }
      return 0;
    }
    
    
    Conversion OK
    String=abc, numbers=12,34,567
    How do I ignore the whitespaces, say if they were deliminated by tabs instead?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Tabs are whitespace as far as sscanf is concerned.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 01:52 AM
  2. JOB: C/C++ Developer with problem solving skills
    By VoltRecruiter in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 01-26-2006, 12:25 AM
  3. Solving A Maze Using C Language!!!!
    By jonnybgood in forum C Programming
    Replies: 6
    Last Post: 11-08-2005, 12:15 PM
  4. Math: solving outcomes...
    By Oktos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-10-2003, 06:55 AM
  5. Sign-up Thread: Problem Solving #1
    By ygfperson in forum Contests Board
    Replies: 15
    Last Post: 01-26-2003, 02:55 AM