Program to find number of lines/paragraphs, and words in a txt file

In summary, the conversation discusses a program that is designed to find words, lines, and paragraphs in a text file. The program scans the document character by character and counts the number of words in each line. If there are no words, it is assumed to be the end of a paragraph. The code for the program is provided, but there seems to be an issue with it not stopping. Suggestions for solving this issue are discussed.
  • #1
FOIWATER
Gold Member
434
12

Homework Statement


Hello,

I need to write a program which finds all words, lines, and paragraphs in a text file.

I am to scan the document character by character. When I get to the end of a line, I need to count the total number of words in that line and add it to the total. If the total is zero, it assumes that was the end of a paragraph.

I wrote the following program to do this.. it successfully scans any txt document and copies it, but it never stops.

it seems to me, that it never sees the end of a line and so, never exits the while(nextChar!=''\n') loop.

Any input appreciated. Here is my code.
Code:
#include <stdio.h>
#include <stdlib.h>

void initialize(int *p1,int *p2,int *p3,int *p4)
{
*p1=0;
*p2=0;
*p3=0;
*p4=0;
}

void processBlank(int *nextChar,int *wordsinLine,FILE *ctPtr)
{
while (*nextChar=='32')   //32 is ascii code for a space.
{
printf("%c",*nextChar);
*nextChar=fgetc(ctPtr);
}
*wordsinLine+=1;
}

void copyText(int *nextChar,FILE *ctPtr)
{
while (*nextChar!='32')
{
printf("%c",*nextChar);
*nextChar=fgetc(ctPtr);
}
}

void updateCount(int *numWords,int *wordsinLine,int *numParagraphs,int *numLines)
{
*numWords+=*wordsinLine;
if (*wordsinLine==0)
*numParagraphs+=1;
*wordsinLine=0;
*numLines+=1;
}

void printTotal(int numWords,int numLines,int numParagraphs)
{
printf("\n\n\n\nTotal number of words is: %d\n\n",numWords);
printf("Total number of lines is: %d\n\n",numLines);
printf("Total number of paragraphs is: %d\n\n\n\n",numParagraphs);
}

void main()
{
int numWords,numLines,numParagraphs,wordsinLine;
initialize(&numWords,&numLines,&numParagraphs,&wordsinLine);
FILE *ctPtr;
int nextChar;
if ((ctPtr=fopen("Q2read.txt", "r"))==NULL)
printf("File could not be opened\n");
else
{
nextChar=fgetc(ctPtr);
while (nextChar!=EOF)
{

while (nextChar!='\n')
{
processBlank(&nextChar,&wordsinLine,ctPtr);
copyText(&nextChar,ctPtr);
}
updateCount(&numWords,&wordsinLine,&numParagraphs,&numLines);
}
printTotal(numWords,numLines,numParagraphs);
fclose(ctPtr);
}
}
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
not sure why you're quoting '32', but it won't help.
 

Related to Program to find number of lines/paragraphs, and words in a txt file

1. How does the program count the number of lines in a txt file?

The program uses the readlines() method to read the entire text file and returns a list of all the lines. The length of this list is then counted to determine the number of lines in the file.

2. Can the program differentiate between blank lines and non-blank lines?

Yes, the program can differentiate between blank lines and non-blank lines. It uses a conditional statement to check if each line in the file is empty or not, and only counts the non-empty lines.

3. How does the program count the number of paragraphs in a txt file?

The program counts the number of times a blank line occurs in the file. Each blank line signifies the end of a paragraph, so by counting the number of blank lines, the program can determine the number of paragraphs in the file.

4. Does the program take into account special characters or punctuation marks when counting words?

Yes, the program takes into account special characters and punctuation marks when counting words. It uses regular expressions to remove all non-alphanumeric characters before counting the words in the file.

5. Can the program work with different file formats, such as .doc or .pdf?

No, the program is designed to work specifically with .txt files. Different file formats may have different structures and formatting, making it difficult for the program to accurately count lines, paragraphs, and words.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top