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

  • Thread starter Thread starter FOIWATER
  • Start date Start date
  • Tags Tags
    File Program
Click For Summary
SUMMARY

The forum discussion centers on a C program designed to count the number of words, lines, and paragraphs in a text file. The user encountered an issue where the program fails to terminate due to an incorrect condition in the while loop that checks for the end of a line. Specifically, the character comparison uses '32' instead of the correct ASCII representation for a space (' '). The program successfully reads and processes text files but requires debugging to ensure it recognizes line endings and counts correctly.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with file handling in C
  • Knowledge of ASCII character encoding
  • Basic concepts of loops and conditionals in programming
NEXT STEPS
  • Debug the program to replace '32' with ' ' for space detection
  • Implement error handling for file operations in C
  • Explore character-by-character file reading techniques in C
  • Learn about memory management and pointer usage in C programming
USEFUL FOR

C programmers, students learning file I/O operations, and anyone interested in text processing algorithms in C.

FOIWATER
Gold Member
Messages
434
Reaction score
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
not sure why you're quoting '32', but it won't help.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
1
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K