-7

I have following problem:

// Basically I am reading from a file and storing in local array.

char myText[100] = "This is text of movie Jurassic Park";

// here I want to store each work in to dictionary

st.insert(&myText[0]); // should insert "This" not till end of sentence.

// similarly for next word "is", "text" and so on.

How do I do that in C?

Tlacenka
  • 520
  • 9
  • 15
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
  • 3
    With 2878 rep you should be able to find out by yourself. Downvote is not from me. – Jabberwocky Aug 14 '15 at 13:38
  • 1
    Is `insert` a pointer to a function in a `struct` of which `st` is an instance? – Bathsheba Aug 14 '15 at 13:40
  • 1. Define "word" (either in terms of Consisting-Of or Consisting-Not-Of). 2. Loop over your input until you get to the start of a word. 3. Loop over your input until you come to the end of a word. 4. ... profit! – Jongware Aug 14 '15 at 13:41
  • 2
    Look for examples using `strtok` or `strtok_r`. – Klas Lindbäck Aug 14 '15 at 13:43
  • This question is not at all clear. Are you saying you want to convert your C++ code to just C? And what is your comment saying about "should insert not"? Please rephrase that to express what it is you want. – donjuedo Aug 14 '15 at 13:43
  • @MichaelWalz Without taking any sides, let me just point out that the gained reputation doesn't have to be related to knowledge of a particular programming language. StackOverflow covers many areas. – Tlacenka Aug 14 '15 at 14:07
  • @Tlacenka you are right, but someone with rep should be able to to do basic google searches, for example "Splitting a string into words c". – Jabberwocky Aug 14 '15 at 14:09
  • possible duplicate of [Split string with delimiters in C](http://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c) – Bo Persson Aug 14 '15 at 14:27

3 Answers3

3

For this, you would use the strtok function:

char myText[100] = "This is text of movie Jurassic Park";
char *p;
for (p = strtok(myText," "); p != NULL; p = strtok(NULL," ")) {
    st.insert(p);
}

Note that this function modifies the string it's parsing by adding NUL bytes where the delimiters are.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

You could use strtok(). http://www.cplusplus.com/reference/cstring/strtok/

For C you will need to include .

0

If you just want to split on spaces, you basically may want an strsplit or strtok. Have a look at Split string with delimiters in C

Community
  • 1
  • 1