0

How can I convert a paragraph into sentences? I have a function signature as follows:

char **makeSentences(char *paragraph);

In which:

  • paragraph is a string containing several sentences. Paragraph ensures that each sentence ends with a period (.) and the whole paragraph ends with a null-terminator.
  • returns a dynamically allocated array of sentences.

I'm a bit confused of how to allocate the memory for the array of sentences on the fly.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
antiopengl
  • 119
  • 3
  • 11

3 Answers3

1

Within makeSentences you will need to examine paragraph to determine the number of characters each sentence contains, including the period and a character for \0.

You can allocate the memory like this:

int i;
char** sentences = (char**)malloc( sizeof(char*) * number_of_sentences );
for (i = 0; i < number_of_sentences; i++) {
    sentences[i] = (char*)malloc( sizeof(char) * length_of_sentences[i] );
}

where length_of_sentences is an array containing the sentence lengths you've discovered.

James
  • 2,373
  • 18
  • 16
0

Write a helper method to provide a count for the number of paragraphs in advance, and then malloc an array of char* pointers based on this number. It will be something like this although the below code is untested so it might not work straight off:

int getSentenceCount( char* paragraph) {
   int sentenceCount = 0, i = 0;
   for(i=0; i < sizeof(paragraph); i++){
        if(paragraph[i] == '.') sentenceCount++;
   }

   return sentenceCount;
}

Once you've got a sentence count the rest should be easy using string utility functions (See string.h)

0

So that the caller can determine when it reached the end of the array, just make the array one entry larger and use NULL for the last/extra entry.

The hard part is definitely going to be determining where sentences start/end. Here's some example sentences (one sentence per line):

Hello world!
Are you hungry?
She said "Hello. I'm Sara!" and smiled.
I installed version 1.3.
They laughed at the F.B.I. and ran.
The function returned the sequence 1, 3, 5, ..., 33.
7 is a lucky number (and this sentence doesn't start with a capital letter).

Put all these sentences into a single paragraph to see if your "end of sentence" detection works correctly.

Brendan
  • 35,656
  • 2
  • 39
  • 66