getting values from strtok

This is a discussion on getting values from strtok within the C Programming forums, part of the General Programming Boards category; Hi! This is some text at page 100 As result from the strtok i get 7 strings like: This is ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    getting values from strtok

    Hi!

    This is some text at page 100

    As result from the strtok i get 7 strings like:

    This
    is
    some
    text
    at
    page
    100

    1.) how can i set some var to each string ?

    e.x.
    This -->a
    is -->b
    some -->c
    text -->d
    at -->e
    page --> f
    100 -->g

    Why am i doing this ?. Becuse i want to be able to compare each string with my defined string and each number with my defined number. The text will NOT be the same so sometimes the number can end up on second place.

    2.) I use strcmp for text comparation but how can i compare numbers ( specialy if i dont know on witch place they will be ) ?

    Thank you !

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    is this somewhat what your looking for for question 1:
    Code:
    #include <stdio.h> 
    #include <string.h>
    
    int main(void)
    {
      char* sentence = "one two three four";
      char* word[4];
      int i = 0;
      
      word[i] = strtok(sentence," ");
    
      while (word[i] != NULL)	  
      {
         i++;
         word[i] = strtok(NULL, " ");
      }
      
      for (i = 0; i < 4; i++)
    	  printf("word[&#37;d] = %s\n",i,word[i]);
      
      return 0;
    }
    for question 2 here is an example, if im understanding:
    Code:
    #include <stdio.h> 
    #include <string.h>
    
    int main(void)
    {
       char* word1 = "hi"; //tokenized string
       char* word2 = "10"; //tokenized string
       char* num = "10"; // number to search for 
      
       // strcmp returns zero if the two strings are equal
       printf("%d\n",strcmp(word1,num));
       printf("%d\n",strcmp(word2,num));   
      
      return 0;
    }
    your using C functions (strtok and strcmp).. is this really a C++ program? if your using actual C++ String objects, it would be easier to just use all C. If you have to stick with C++, im sure there are better ways to do this that are C++-specific.. check it out.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Hi !
    Im using C and this is the part that im using now:

    Code:
    char string[30];
    gets(string);  
    char *tok;
    tok = strtok(string," ");
    while (tok != NULL)
    {
    printf("\nvalue: &#37;s",tok);
    tok = strtok(NULL," ");
    }
    // this is the part where i need to make comparation
    If you enter this:

    Im entering text and number 100

    you will get:

    Im
    entering
    text
    and
    number
    100

    All words that i want to compare with the user input are defined in var like this:
    Code:
    string  wordOne[10]= "This";
    string  wordTwo[10]= "is";
    Now, i need to see if the user has entered number ( isdigit ) and to see if the input is the same as the before defined words (wordOne,wordTwo ).

    Im would do this thru:
    Code:
    if(strcmp(wordOne,string ) == 0)
    but i dont know how to pull out and compare each word that is comming out from the strtok result.
    Last edited by c++Newbee; 04-13-2007 at 03:11 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Moved.

    > word[i] = strtok(sentence," ");
    Trying to strtok() a string constant such as this is really bad, since strtok() will modify the string (or at least try to in this case).

    > gets(string);
    Read the FAQ on why gets() is bad.
    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
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    > word[i] = strtok(sentence," ");
    Trying to strtok() a string constant such as this is really bad, since strtok() will modify the string (or at least try to in this case).
    Ditto for char* pointers (instead of arrays):
    Code:
    char* sentence = "one two three four";
    > gets(string);
    Read the FAQ on why gets() is bad.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Hi guys and thanx for replay!

    What kind of solution can you provide for my problem with comparation and number recognition ( written as original topic ) ?

    c++Newbee

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    My solution is "Do your own ........ing homework"! Where's your code? I don't see you doing anything here other than asking people to do it for you.


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

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    @quzah

    Im sorry if i offend you in any way.
    Please look my next to the last post (Yesterday 09:36 AM) and there you will find the part that i have made and the part that i dont know how to make.


    Thank you in advance.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to walk through the elements in an array? Strings end in a null character. When you reach one, you know you can stop. Walk through your array testing each character for whatever it is you need. Once you have this concept down, move on to something bigger, like searching for series of characters in a string.

    Just make a small program that reads a string, walks through it, and gives you the number of ... whatever ... that it contains. Maybe then edit it so it counts up the number of each different thing in it. (For example, if you enter "that", it will show you 2 t, 1 h and 1 a.)

    When you get stuck along the way, post a small example (usually small self-contained examples are best) and explain what it is you're trying to do, what you can't do, that sort of thing.


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

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    This is what i know :

    Code:
    void tokenArr(char *theString,char *charArr[10],char *chDel);
    void showArray(char *charArr[10]);
    
    int main()
    {
          char *varC[10];
    	  char String[30];
          char *token ;
          
          gets(String);
    	  tokenArr(String,varC," ");
    	  showArray(varC);
    	  return 0;
    }
    
    void tokenArr(char *theString,char *charArr[10],char *charDel)
    {
        	char *token;
        	int i = 0;
        	
        	token = strtok(theString," ");      // first token
        	charArr[i] = token;                 // save in array
        	i++;
        
        	while (token != NULL)
            {
        		token = strtok(NULL, charDel);
        		charArr[i] = token;
        		i++;
        	}
    }
    void showArray(char *charArr[10])
    {
            for (int i =0;i < 10;i++)
            {
             //if (strcmp(charArr[i],"This") == 0)         // how to make comparation
             
               printf("\n Entered value is equal to defined value");
               
               //printf("\nvalue: &#37;s",charArr[i]);    
            }
            
    }
    But i dont know how to compare the strings with my defined text ( in my example this is word "This").

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strcmp is case sensitive.


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

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    So when are you going to stop using gets() then?
    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.

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. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 06:01 PM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 07:56 PM

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