How can i fetch word from a string?

i need to fetch all words from char string.

Page 1 of 1

3 Replies - 993 Views - Last Post: 01 April 2010 - 11:58 AM Rate Topic: -----

#1 Guest_erdemekinci*


Reputation:

How can i fetch word from a string?

Posted 01 April 2010 - 11:36 AM

hi

i need to fetch all words from a string that includes a paragraph.

example;

i have these arrays;

char parg[] = "hi everyone. My name is jack.";
char *words[];

and i wanna make my words[] array like this;
char *words[] = { "hi", "everyone", "My", "name", "is", "jack" };

but i coulndt find correct way. i just found a string method named strtok which claimed that it takes first word. but i just could fetch first word.

how can i fetch step by step all words, from a string to another string array? i really need it. that s a kind of word statistic analyser. i have too much things that need to be done. but i coulndt pass this problem. pls if anyone help me about this code or give me any different way to do same thing, it will make ma glad.

p.s: i am really sorry about weak english

Is This A Good Question/Topic? 0

Replies To: How can i fetch word from a string?

#2 Guest_Andrew Tomczak*


Reputation:

Re: How can i fetch word from a string?

Posted 01 April 2010 - 11:44 AM

http://www.cplusplus...cstring/strtok/ ;)
Was This Post Helpful? 0

#3 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: How can i fetch word from a string?

Posted 01 April 2010 - 11:46 AM

You can use strtok to find other words, you just have to remember the first time, the first parameter points to the string. To find the next token, the first parameter must be NULL. Example:


   char * s = strtok("My name is Martyn"," ");
   printf("%s\n", s);
   while ( (s = strtok(NULL, " ")) != NULL ) {
       printf("%s\n", s);
   }



Hope that helps you.
Was This Post Helpful? 2
  • +
  • -

#4 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How can i fetch word from a string?

Posted 01 April 2010 - 11:58 AM

Take a look at msdn.

This post has been edited by Tapas Bose: 01 April 2010 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1