Thread: something like strtok

  1. #16
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    i fix it
    Code:
         char *new;
        
        i=0;
        len=strlen(t);
       while(i<len && t[i]!='\0'){
            if(t[i]== ' '){
              last=i;
              t[i]='\0';
              printf(t);
              }
              i++;
              new = &t[i];
              }
    and now it prints me only hello
    can i drop it immediatly on the list know or i should use another array?
    and i use pointer new to catch the new adress of the string "wor"
    but how will make this for any string? I mean in the while loop in the if statement know i should check for the pointer new if has a space?
    Last edited by alzar; 01-18-2008 at 07:55 AM.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, just strcpy() to copy the string. But next you need to move the looking for next space by looking from "t[i]" onwards. So you probably should introduce another char pointer, which starts at the beginning of "t", then moves to "t[i]" when the space has been found.

    I would also use a for-loop, rather than a while-loop, since you are just counting up i all the time. Also you need to copy the last string, even if you don't find a space.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    i make a try but it doesn't work. I am confused with the loop.
    Code:
    int len,l;
        char t[23]="hello world how are you";
        int i, last,count;
        char *words[10];
        char *new;
    
    
    len=strlen(t);
    new=t;
    
    for(i=0; i<len; i++)
      if(new[i]==' '){
         t[i]='\0';
         len=strlen(t);/*so this changes also the len in the loop*/
         words[count]= malloc(len2*sizeof(char));
         strcpy(words[count],t);
         count++;
         new=&t[i+1];
         }
    }
    any help?

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I used your program as a template, and came up with this. It's not very elegant, but it's clear what it's doing, I think.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)  {
      int len,l;
      char t[]="hello world how are you";
      int c, i, last, count;
      int offset = 0;
      char *words[10] = {0};
      char *new;
    
      len=strlen(t);
    
      for(i = 0, count = 0; i <= len; i++)
        if(t[i]==' ' || i == len) {
          t[i]='\0';
          
          if(offset == 0) 
            words[count] = malloc(i * sizeof(char));
          else 
            words[count] = malloc(offset * sizeof(char));
            
          strcpy(words[count], t + offset);      
          count++;
          offset = i + 1;
       }
       for(i = 0; i < count; i++)
         printf("\n &#37;s", words[i]);
    
       printf("\n\n Press Enter When Ready ");
       c = getchar();
    
       return 0;
    }
    Last edited by Adak; 01-18-2008 at 04:27 PM.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    sizeof(char) is always 1, and I have a sneaking suspicion that you're malloc()'ing one less byte than you ought to be . . . .

    Oh, and l (lowercase L) is an absolutely stupid name for a variable -- it looks just like a 1.

    (@Adak: I realize that most of your code was based on alzar's, but still . . . .)
    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. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by dwks View Post
    sizeof(char) is always 1, and I have a sneaking suspicion that you're malloc()'ing one less byte than you ought to be . . . .

    Oh, and l (lowercase L) is an absolutely stupid name for a variable -- it looks just like a 1.

    (@Adak: I realize that most of your code was based on alzar's, but still . . . .)
    That's funny, I removed several variables, added one's that made sense to me, name-wise, and never once even noticed the lower-case L. I don't use that for a variable name.

    The malloc length is right in very limited testing. The size reflects that the cp is either at the space AFTER the word, or at the NULL AFTER the last word. The last NULL gets written over and replaced by another NULL, but it wasn't worth changing the logic for that, imo.

    I didn't try that code out on other sentences/words, so I ain't a guaranteeing nuttin'!

    Student's beware!!

  7. #22
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Thank you so match Adak i try and other sentences and it seems to work fine.It was so easy.I was trying with the pointer new and you didin't need to use this pointer.Thanks!

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. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 10:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM