Looking at strings

This is a discussion on Looking at strings within the C Programming forums, part of the General Programming Boards category; (in C and only in C) hi, im reading from a file a number of items, eg.... add(15); sum(); add(100); ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Looking at strings

    (in C and only in C)
    hi, im reading from a file a number of items, eg....
    add(15);
    sum();
    add(100);
    sum();
    ok.......
    I am tokenizing the file then putting the tokens in an array. I then want to look at each token and then get the value from with the brackets of the add tokens

    eg add(50) value = 50.

    Cant think of a way to do this, can someone please help??? with a bit of code i could use?

    Thankyou very much in advance

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    15
    Well, I won't do your homework for you but I'll give you an idea where you could start.

    I would start by making functions for the strings you read in... like a function for add(), sum(), etc

    And then in the main method, I would actually do the reading from the file and call the methods it asks for.

    Alexisa

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    ***THIS ISNT A HOMEWORK QUESTION*******

    Ok let me get rid of some of the extra detail and ask a simplier question,

    i have a string, there is(definatley) a number within the string, how can i find out the number???

    eg


    ifthenumberinthebracketsis50iwillbeveryhappy(50);

    This dos not mean that ifthenumberinthebracketsis50iwillbeveryhappy is a function but just a string.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    You could start with the strchr() function to locate the positions of the parentheses
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or perhaps a simple loop using isdigit to test each character for validity.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by quzah
    Or perhaps a simple loop using isdigit to test each character for validity.

    Quzah.
    Another option: you can use strcspn() to achieve a similar result.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by quzah
    Or perhaps a simple loop using isdigit to test each character for validity.

    Quzah.
    That won't work based on his example. Find the open paren and test/read the number.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WaltP
    That won't work based on his example. Find the open paren and test/read the number.
    Sure it would.
    Code:
    for( x = 0; s[x]; x++ )
    {
        if( x > 0 && isdigit( s[x] ) && s[x-1] == '(' )
            printf("See? It works. You just have to be creative.\n");
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by quzah
    Sure it would.
    Code:
    for( x = 0; s[x]; x++ )
    {
        if( x > 0 && isdigit( s[x] ) && s[x-1] == '(' )
            printf("See? It works. You just have to be creative.\n");
    }
    Quzah.
    OK, smartypants, try it on
    ThisIsA30CharacterFunctionName(15) without the paren check, as you implied before!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WaltP
    OK, smartypants, try it on
    ThisIsA30CharacterFunctionName(15) without the paren check, as you implied before!
    I didn't say anything about a paren check. I said a simple loop using isdigit. My last example fits that description.


    Quzah.
    Hope is the first step on the road to disappointment.

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, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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