Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct node{
- char* string;
- struct node* next;
- }Node_t;
- //reading and creating
- void readFile(FILE* fp, Node_t* currentPtr);
- void printList(Node_t* head);
- //creating the nodes
- //freeing nodes
- Node_t* createNode(char* string);
- void freeMemory(Node_t* head);
- Node_t* insert(Node_t* currentPtr, char* string);
- int calculateSpaces(Node_t* head);
- void printUpdatedList(Node_t* head, int justify, int spaces, int numWords);
- //maybe we can do snazzy stuff if this is done quick enough
- void removeNewLines(Node_t* head);
- int main()
- {
- char fileName[100];
- printf("Please enter the file to open (no spaces please): ");
- scanf("%s", fileName);
- printf("\n");
- /* generate new file pointer */
- FILE* fp = NULL;
- if((fp = fopen(fileName, "r")) == NULL){
- printf("Error, could not open file\n");
- exit(1);
- }
- Node_t* head = NULL;
- readFile(fp, head);
- return 0;
- }
- void readFile(FILE* fp, Node_t* head){
- /*allocate memory for a new string pointer */
- char* word = malloc(100*sizeof(char));
- /* fgets to get each line with max buffer size of 100 chars */
- while(fgets(word, 100, fp)){
- /* tokenize each word */
- char* token = strtok(word, " ");
- while(token != NULL){
- /* insert each token into linked list */
- head = insert(head, token);
- token = strtok(NULL, " ");
- }
- }
- removeNewLines(head);
- int spaces = calculateSpaces(head);
- // printList(head);
- }
- Node_t* insert(Node_t* head, char* string){
- /* allocate memory for new node */
- Node_t* temp = malloc(sizeof(Node_t));
- /* if new pointer cannot be created */
- if(temp == NULL) {
- printf("Error could not allocate memory....");
- return NULL;
- }
- /* allocate memory for new data to be added */
- temp->string = malloc(100*sizeof(char));
- /* if head is empty create new stringcpy */
- if(head == NULL){
- strcpy(temp->string, string);
- temp->next = NULL;
- head = temp;
- } else {
- /* save the head */
- Node_t* currPtr = head;
- /* move to the last node */
- while(currPtr->next != NULL){
- currPtr = currPtr->next;
- }
- strcpy(temp->string, string);
- temp->next = NULL;
- currPtr->next = temp;
- }
- /* return starting node */
- return head;
- }
- void printList(Node_t* head){
- while(head->next != NULL){
- printf("%s ", head->string);
- head = head->next;
- }
- }
- void removeNewLines(Node_t* head){
- int ln = 0;
- Node_t* currPtr = head;
- while(currPtr->next != NULL){
- ln = strlen(currPtr->string) - 1;
- if(currPtr->string[ln] == '\n'){
- currPtr->string[ln] = '\0';
- }
- currPtr = currPtr->next;
- }
- }
- int calculateSpaces(Node_t* head){
- int justifyAmount;
- printf("Enter a jusitfy amount: (between 40 and 100) ");
- scanf("%d", &justifyAmount);
- int totalStrLen = 0;
- int tempVal = 0;
- Node_t* currPtr = head;
- Node_t* tempList = NULL;
- int spaces = 0;
- int words = 0;
- int tempJust = 0;
- while(currPtr->next != NULL){
- tempVal = totalStrLen;
- if((tempVal += strlen(currPtr->string)) >= justifyAmount){ //sentence is too big
- spaces = words - 1;
- tempJust = justifyAmount - totalStrLen;
- printUpdatedList(tempList, tempJust, spaces, words);
- totalStrLen = 0;
- tempJust = 0;
- spaces = 0;
- words = 0;
- free(tempList);
- tempVal = 0;
- tempList = NULL;
- }else{
- tempList = insert(tempList, currPtr->string);
- totalStrLen += strlen(currPtr->string);
- words++;
- }
- currPtr = currPtr->next;
- }
- }
- void printUpdatedList(Node_t* head, int justify, int spaces, int numWords){
- int i = 0;
- int j = 0;
- int tempLen = 0;
- //printf("justify amount: %d\n", justify);
- int justifyPerSpace;
- Node_t* currPtr = head;
- if(spaces != 0) {
- justifyPerSpace = justify / spaces;
- } else { }
- //do nothing (this is the empty line)
- while(i < numWords && currPtr != NULL) {
- tempLen += strlen(currPtr->string);
- printf("%s", currPtr->string);
- for(j = 0; j < justifyPerSpace; j++) {
- printf(" ");
- }
- currPtr = currPtr->next;
- i++;
- }
- printf("\n");
- }
- void freeMemory(Node_t* head){
- while(head->next != NULL){
- Node_t* temp = head;
- head = head->next;
- free(temp);
- }
- free(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement