Thread: Breaking down into even more tokens?

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Breaking down into even more tokens?

    Hello all,
    I am making a program that can solve for x. I have already made the program break the string into individual parts using strtok(). However, I don't know how to make the code that checks an individual token for x. For instance if I have 54x + 8 = 22. The program can break it into the tokens 54x, +, 8, = 22, but I don't know how to make the program recognize that 54 has an x in it. Any help would be appreciated.

    Thank you for reading this message

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Look into the strchr() function in string.h, it will return a pointer to the first occurrence of the character or NULL if not found. It's also possible to create a similar function, that checks if each char in the string is equal to 'x'.

    Code:
            char *found = strchr(argv[1], 'x');
            if(found)
                    printf("%c\n", *found);
            else
                    printf("Not found");

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    These sort things can be very easly be solved using the state machine. But here is an simple solution

    Code:
    char exp[] = "54x"; 
    char V;
    int d;
    
    if( sscanf( exp, "%d%c", &d, &V) == 2 )
        print d, V
    else
       print FAILED
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best method for counting tokens
    By Sn0wcra5h in forum C Programming
    Replies: 3
    Last Post: 02-21-2010, 07:13 PM
  2. Breaking string into tokens
    By cutelucks in forum C Programming
    Replies: 4
    Last Post: 04-27-2007, 08:36 AM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Splitting A String Into Tokens!
    By bobthebullet990 in forum C Programming
    Replies: 15
    Last Post: 03-22-2006, 09:24 AM
  5. Tokenising Tokens..
    By StanleyC in forum C Programming
    Replies: 2
    Last Post: 06-04-2004, 10:53 AM