Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program I am trying to write reads in a txt file and then prints the txt in

ID: 3621106 • Letter: T

Question

The program I am trying to write reads in a txt file and then prints the txt in a type-justified format. I've got the program working to the extent that it reads in a file that has txt's that dont have any paragraphs in them but rather just a single paragraph. I need to have it read in for multiple paragraphs. For this program a new paragraph is denoted with two ' '. My code is provided below and if anyone could figure out the code I need to add in order to read in multiple paragraphs I would be greatly appreciative.

My Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

struct node
{
char *word;
struct node *next;
};

struct node *alist = NULL;
struct node *end = NULL;
struct node *begin = NULL;
struct node *aPtr = NULL;

void LinkedList(struct node *j, struct node **last);
void readfile(int numchar);
void freeMemory();
void printList(int numchar);



int main()
{

int cPerLine = 0;

printf("Enter how many characters you want per line between 55 and 80. ");
scanf("%d", &cPerLine);
readfile(cPerLine);
printList(cPerLine);
freeMemory();
return 0;
}

void readfile(int numchar)
{

FILE *filePtr = NULL;
char c1, c2;
char *String = malloc(30*sizeof(char));
char filename[20];

printf("Enter the name of the file containing your txt. ");
scanf("%s", filename);
if((filePtr = fopen(filename, "r")) == NULL)
{
printf("File can't be opened ");
exit(1);
}
else
filePtr = fopen(filename, "r");
fscanf(filePtr, "%s", String);
alist = (struct node *)malloc(sizeof(struct node));
alist->word = String;
alist->next = NULL;
end = alist;

while(!feof(filePtr))
{
String = malloc(30*sizeof(char));
fscanf(filePtr, "%s", String);
end->next = (struct node *)malloc(sizeof(struct node));
end = end->next;
end->word = String;
end->next = NULL;

}
fclose(filePtr);
printf(" This is the text ");
}

void printList(int numchar)
{
end = alist;
aPtr = alist;
begin = alist;

while(begin->next != NULL)
{
int numSpace[80] = {};
int NumWSpace = 0;
int charLine = 0;
int leftOverSpace = 0;
int counter = 0;
int finalLine = 0;
int j = 0;
int i = 0;

charLine = strlen(end->word)+1;
counter = 1;
end = end->next;

while(charLine+strlen(end->word)+1 < numchar)
{
charLine = charLine + strlen(end->word) + 1;
counter = counter + 1;
if(end->next != NULL)
end = end->next;
else
{
finalLine = 1;
}
}
leftOverSpace = numchar - (charLine-1);

while(leftOverSpace > 0)
{
if(NumWSpace < (counter - 1))
{
numSpace[NumWSpace] = numSpace[NumWSpace] + 1;
leftOverSpace = leftOverSpace - 1;
NumWSpace = NumWSpace + 1;
}
else
NumWSpace = 0;
}

//This loop will pring out the words for the line it is on
for( i = 1; i <= counter; i++)
{
if(i , counter)
{
printf("%s ", aPtr->word);
for(j = 1; j <= numSpace[i-1]; j++)
{
printf(" ");
}
}
else if(i = counter)
{
printf("%s ", aPtr->word);
}
if(aPtr->next != NULL)
aPtr = aPtr->next;
}
if(aPtr->next != NULL)
end = aPtr;
if(end->next == NULL)
begin = end;
printf(" ");
}
}

void freeMemory()
{

end = alist;
while(end->next != NULL)
{
end = end->next;
}
while(alist != NULL)
{
if(end == alist)
{
free(alist->word);
free(alist);
alist = NULL;
end = NULL;
aPtr = NULL;
}
else
{
aPtr = alist;
while(aPtr->next != end)
aPtr = aPtr->next;
free(end->word);
free(end);
end = aPtr;
}
}
}

Example Text:

This is the txt before it gets aligned:

A knowledge and appreciation of interrelationship between places and events
is essential if we are to make informed decisions about their place in the
world community. Therefore,
the extent of basic geographic and historical literacy may play crucial role in decision
making in global international markets.

------------------------------------------------------------------------------------------------------

The txt is supposed to be aligned with the number of characters per line that the user has specified. The spaces are distributed accordingly if needed depending on if the spaces are needed to fill out the number of characters per line. This happens when a word is to long to fit into the characters per line that was specified by the user.

Explanation / Answer

Since you're specifically told that is a new paragraph, have you tried using a look ahead when reading the file? Or are you having a different problem than just locating the new paragraph? They way I would do it if you're not concerned with storage space is when you bring in 1 character/word at a time, store it in an array. Then you parse the array printing it. When you find a then use a look ahead to view the next element. If it's a you know the element right after the second is the beginning of your next paragraph.