Thread: Help using strtok

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Help using strtok

    Hello.

    Can some one help me, using strtok and some checks.
    Code:
    int num_args()
    {
    int numero=0,size=0
    char op[20]="p 1 1",char op1[20]="p 10 20";
    char *pch;
    size=strlen(op);
    printf("SIZE->%d\n",size);
    pch = strtok (op," ");
    	while (pch != NULL)
    	{
    	numero++;
    	pch = strtok (NULL, " ");
    	}
    return numero;
    }
    this is the pure example of strtok. I already tried so many times but i so noob i can figure it out.

    I need so check is first token is always letters (already try using isalpha) and the other two tokens are only digits (isdigit).
    can some one make some lights over my head

    This is an "small game" and i need to check with piece want to play (letter) and corrdinates x and y.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    I don't see your attempt? isdigit and isalpha seem like fine choices, you should be able to get the job done using those.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    My problem is that pch will have at first time an letter so is digit will work fine, but next time will have numbers, and i can figured out how i will do the job so it will works for everithing.
    This because the user can input p 1a 22 and should return an error of coordinates.
    Its possible to pass the resulte of strtok to 3 diferents variables (like piece, x and y).

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you want to check the "1a" string represents the number -
    you can use strtol and check the end pointer is pointing to 0 char after convertion... if not - some garbage was left in the string
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Here is an example using strok which saves 3 pointers. Note that these are just pointers to places within the original string, ie if you change the original string you will be changing what they are pointing to as well.

    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main(void)
    {
        char s[] = "p1 23 5a5";
        char * tok[3], * p;
        int i;
    
        p = strtok(s," ");
        for (i = 0; p && i < 3; i++) {
            tok[i] = p;
            p = strtok(NULL," ");
        }
    
        if (i < 3) {
            fprintf(stderr,"Missing parameters\n");
            return -1;
        }
    
        return 0;
    }
    You could then check each token for what it should be... here is a simple routine you could use to check a string against any is*() function:

    Code:
    int checkstr(const char * s, int (*func)(int))
    {
        int r = 1;
    
        if (s) 
            while (*s) 
                if (!(r = func(*s++))) 
                    break;
    
        return r;
    }
    You would then call it like so: checkstr(string,isalpha) or checkstr(string,isdigit)

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Hello nonpuz.
    Thanks for the help...it's working great.
    I changed a little because it was very advanced for me, but i got the idea so it was easy to change.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  3. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  4. strtok tokenizing on spaces as well as my delimiter
    By snowblind37 in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2004, 12:39 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM