Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Define three functions: the first function to extract all the sentences, the second to determine the longest sentence, and the third to determine the average sentence length.</p> <p><strong>Extract all sentences</strong></p> <pre><code> ''' &lt;summary&gt; ''' Extracts all sentences from a text block. It is assumed that a sentence is terminated by either a period (.), a question mark (?), or an exclamation mark (!). ''' &lt;/summary&gt; ''' &lt;param name="text"&gt;The text block as string.&lt;/param&gt; ''' &lt;returns&gt;An array of sentences.&lt;/returns&gt; ''' &lt;remarks&gt;&lt;/remarks&gt; Function sentences(ByVal text As String) As String() Dim snts(Len(text)) As String Dim curPeriodPos As Integer Dim nextPeriodPos As Integer Dim longestSnt As String Dim nextSnt As String Dim i As Integer text = text + " " text = Replace(text, "? ", ". ") text = Replace(text, "! ", ". ") curPeriodPos = InStr(text, ". ") longestSnt = Mid(text, 1, curPeriodPos) i = 0 Do While curPeriodPos + 1 &lt; Len(text) nextPeriodPos = InStr(curPeriodPos + 1, text, ". ") nextSnt = Mid(text, curPeriodPos + 1, nextPeriodPos - curPeriodPos + 1) snts(i) = nextSnt i += 1 curPeriodPos = nextPeriodPos Loop Return snts End Function </code></pre> <p><strong>Determine longest sentence</strong></p> <pre><code> ''' &lt;summary&gt; ''' Determines the longest sentence among an array of senetences. ''' &lt;/summary&gt; ''' &lt;param name="sentencesArray"&gt;A string array of sentences.&lt;/param&gt; ''' &lt;returns&gt; A sentence as string.&lt;/returns&gt; ''' &lt;remarks&gt;&lt;/remarks&gt; Function longestSentence(ByVal sentencesArray() As String) As String Dim longestSnt As String = "" For Each sentence As String In sentencesArray If Len(sentence) &gt; Len(longestSnt) Then longestSnt = sentence End If Next sentence Return longestSnt End Function </code></pre> <p><strong>Determine average sentence length</strong></p> <pre><code> ''' &lt;summary&gt; ''' Determines the average sentence length of sentences in an array of sentences. ''' &lt;/summary&gt; ''' &lt;param name="sentencesArray"&gt;A string array of sentences.&lt;/param&gt; ''' &lt;returns&gt;An integer representing the average sentence length.&lt;/returns&gt; ''' &lt;remarks&gt;&lt;/remarks&gt; Function averageSentenceLength(ByVal sentencesArray() As String) As Double Dim sentenceLengths(sentencesArray.Length) As Integer Dim total As Integer = 0 Dim average As Double Dim counter As Integer = 0 For i As Integer = 0 To sentencesArray.Length - 1 sentenceLengths(i) = Len(sentencesArray(i)) If sentenceLengths(i) &gt; 0 Then total += sentenceLengths(i) counter += 1 End If Next i average = CDbl(total) / CDbl(counter) Return average End Function </code></pre> <p>These functions can be called in the buttons's event handlers as follows:</p> <pre><code>Private Sub DisplayLongestSentence() Handles longestSententeceButton.Click Dim text As String = TextBox1.Text MessageBox.Show(longestSentence(sentences(text))) End Sub Private Sub DisplayAverageSentenceLength() Handles averageSententeceLengthButton.Click Dim text As String = TextBox1.Text MessageBox.Show(averageSentenceLength(sentences(text))) End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload