How Can I Find the Paragraph Number On File

favorman.txt contains



like this.
Output =4
How can i find this

I would start by defining what “paragraph” means to your application. How much control do you have over the data format? Is a paragraph some text followed by a line with only a newline? Can you just count the number of blank lines and add one? What if the file only had “aaaa” in it – would that be one paragraph, or zero? What if there are multiple blank lines in a row?

Mr. Bennet made no answer.

“Do you not want to know who has taken it?” cried his wife impatiently.

You want to tell me, and I have no objection to hearing it.”

This was invitation enough.

“Why, my dear, you must know, Mrs. Long says that Netherfield is taken
by a young man of large fortune from the north of England; that he came
down on Monday in a chaise and four to see the place, and was so much
delighted with it, that he agreed with Mr. Morris immediately; that he
is to take possession before Michaelmas, and some of his servants are to
be in the house by the end of next week.”

“What is his name?”

Like this 6 paragraph over there. I don’t count to blank line because sometimes 2 blank lines between paragraphs .

Have you tried implementing this yourself? Do you have any ideas about algorithms to do this?

I have tried during 2 day without sleep

Okay, what did you come up with?

Hey @favorman,

If you are sure that at least one blank line is always a new paragraph, you could just check for anytime there is either 1 or more blank lines. Did you need help working that out?

Here’s an example of my last reply:

package main

import (

func main() {
	f, err := os.Open("file.txt")
	if err != nil {
	defer f.Close()

	paras := []int{}

	scanner := bufio.NewScanner(f)

	var prevEmpty bool
	for lineNo := 0; scanner.Scan(); lineNo++ {
		if strings.TrimSpace(scanner.Text()) == "" {
			prevEmpty = true
		if prevEmpty || lineNo == 0 {
			paras = append(paras, lineNo)
			prevEmpty = false

	// Paragraph line positions.

Thank you so much , got it ! :slight_smile:

Finally i got it thank you for helping :slight_smile:

Anytime :slight_smile:

Although I just realized my example also gives you the positions of the paragraphs when you only actually asked to find out how many paragraphs there actually were, so if you still want the positions, just use len(paras) to get the number, otherwise just replace paras = append(paras, lineNo) with numParas++ or something.

Thanks again . That informations are important for me because You know still no much enough informations on internet about GO programming language

Cool. Anyway, good luck with whatever you are working on :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.