0

I'm trying to do this

Say I have a random text:

rabbit cat dog fish dog
the fish and cat
elephant and fish

and I want to store all the words in the text in the following arrays:

char word[10]; 
char* words_in_sentence[5]; 
char* sentences_in_text[9];

// individual word
// array of words in each sentence
// array of arrays of words in every sentence

How do I store my *words_in_sentence entries to *sentences_in_text?

I'm trying to access it like sentences_in_text[sentence number][word position]

Lundin
  • 195,001
  • 40
  • 254
  • 396
cdpp
  • 152
  • 2
  • 9
  • https://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c may help you – Basya Jun 10 '21 at 09:30
  • 2
    You don't have a 2d array of sentences. You have a 2d array of words. – enhzflep Jun 10 '21 at 09:31
  • Does this answer your question? [How do I create an array of strings in C?](https://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c) – Basya Jun 10 '21 at 09:32
  • @enhzflep could you explain on that? I thought my sentences_in_text was an array with each entry containing a pointer to another char array of multiple words? – cdpp Jun 10 '21 at 10:04
  • @cdpp - Uh-huh, sure. Your code does go about it that way. But it's a poor model. (really, the issue it might be as little as a variable name - or as large as your understanding). What you wish to do, is `words[sentence_index][position_in_sentence]`. You only have a single list of sentences. Therefore, `sentences[index]` is appropriate. However, `position_in_sentence` can be zero more than once. It can be zero for each and every one of em(the sentences), and there can be more than one sentence. Does this help? – enhzflep Jun 10 '21 at 11:06
  • Put another way, think of the words as pixels and the sentences as horizontal lines. Horizontal lines is a 1d array [0..maxRow-1]. Pixels are a 2d one. [0..maxRow-1][0..maxCol-1] – enhzflep Jun 10 '21 at 11:11

0 Answers0