Question

In: Computer Science

WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)

WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

#include "index.h"

/* Text functions area */

FILE *getFile (int argc, char const *argv[]) { /* A function to get the file inclouding a input check */
    FILE *theinput;

    if (argc < 2) { /* no argument */
        fprintf(stderr, "Must pass an argument!\n");
        exit(1);
    }

    theinput = fopen(argv[1], "r");

    if (!theinput) { /* Argument is non-existing file */
        fprintf(stderr, "Can't read %s\n", argv[1]);
        exit(1);
    }

    return theinput;
}

char *getText (FILE *file) { /* Gets the text from file, Return a string with the file content */
    char *text, *q;
    char token;
    int i = 0;
    text = malloc(sizeof(char));
    while ((token = getc(file)) != EOF) {
        text[i] = token;
        i++;
        q = realloc(text, (i + 2) * sizeof(char));
        if (!q) {
            fprintf(stderr, "Out of memory\n");
            exit(1);
        }
        text = q;
    }
    text[i] = '\0';
    return text;
}

bool isLegalChar (char a) { /* Checks if a char is A - Z or a - z or 0 - 9 using ascii */
    if (a <= 90 && a >= 65) { /* A = 65, Z = 90 */
        return true;
    }
    else if (a <= 122 && a >= 97) { /* a = 97, z = 122 */
        return true;
    }
    else if (isdigit(a)) {
        return true;
    }
    else {
        return false;
    }
}

char *getWord (char *text, int start, bool *isEnterFound) { /* returns a single word from text starts searching from start */
    char *word,*q; /* q = a helper string to make sure mallocing well */
    char token; /* token is the right now input char */
    int i = 0;
    int j = start;
    word = malloc(sizeof(char));
    while (isLegalChar(token = text[j++]) || token == '\n') { 
        if (token == '\n')
        {
            (*isEnterFound) = true;
            break;
        }
        word[i++] = token;
        q = realloc(word, (strlen(word) + 2) * sizeof(char));
        if (!q) {
            fprintf(stderr, "Out of memory\n");
            exit(1);
        }
        word = q;
    }
    word[i] = '\0';
    return word;
}

void tolowerString (char *input) { /* Changes a string so all the letters in it will by lower */
    int i;
    for (i = 0; i < strlen(input); ++i)
    {
        input[i] = tolower(input[i]);
    }
}

char* stradd(const char* a, const char* b) { /* Return a string which is the two given strings combined */
    size_t len = strlen(a) + strlen(b);
    char *ret = (char*)malloc(len * sizeof(char) + 1);
    *ret = '\0';
    return strcat(strcat(ret, a) ,b);
}

/* Tree functions area */ 

Node* newNode(char* word, int line) { /* Return a new node with the vars given to the funcion */
    Node *p = (Node*) malloc (sizeof(Node)); 
    p->word = word;
    p->lines = malloc(sizeof(int));
    p->lines[0] = line;
    p->linesLength = 1;
    p->ls = p->rs = NULL; 
    return p; 
}

void insert(Node **root, char *word, int line) { /* Insert node to tree using recursion */
    int *q;
    int i;
    if(!*root) {
        *root = newNode(word, line); 
        return; 
    }
    if(strcmp(word, (*root)->word) < 0)  /* In case word is smaller than the nodes word */
    { 
        insert(&((*root)->ls),word,line); 
    }
    else if (strcmp(word, (*root)->word) == 0) /* In case word already exist */
    {
        for (i = 0; i < (*root)->linesLength; ++i)
        {
            if ((*root)->lines[i] == line)
            {
                return;
            }
        }
        q = realloc((*root)->lines, (*root)->linesLength + 2);
        if (!q)
        {
            fprintf(stderr, "Out of memory!\n");
            exit(1);
        }
        (*root)->lines = q;
        (*root)->lines[(*root)->linesLength] = line;
        (*root)->linesLength++;
        return;
    }
    else /* In case word is bigger than the nodes word */
    {
        insert(&((*root)->rs),word,line); 
    }
}

void printTreeRec(Node *root, FILE *outputFile) { /* A recursive function which prints the tree by given root */
    int i;

    if(!root) {
        return; 
    }
    printTreeRec(root->ls, outputFile);

    fprintf(outputFile, "The word: \"%s\" ",root->word); 
    if (strlen(root->word) < 15) {
        for (i = 0; i < 15 - strlen(root->word); ++i) {
            fprintf(outputFile, " ");
        }
    }   
    fprintf(outputFile, "appears in ");
    if (root->linesLength == 1) {
        fprintf(outputFile, "line: ");
    }
    else {
        fprintf(outputFile, "lines: ");
    }
    for (i = 0;i < root->linesLength - 1; i++) {
        fprintf(outputFile, "%d, ", root->lines[i]);
    }
    fprintf(outputFile, "%d", root->lines[i]);
    fprintf(outputFile, "\n");

    printTreeRec(root->rs,outputFile);
}

void freeTree(Node *root) { /* A function to free the tree recursively */
    if(!root)
        return; 
    freeTree(root->ls);
    freeTree(root->rs); 
    free(root->lines);
    free(root); 
}

/* Main */

int main(int argc, char const *argv[]) {
    FILE *inputFile = getFile(argc,argv); /* Opening input file */
    char *ouputAddress = stradd(argv[1],".index"); /* Creating output file address */
    FILE *outputFile = fopen(ouputAddress,"w"); /* Creating output file */
    char *text = getText(inputFile);
    char *word;
    int line = 1; /* Line counter */
    bool foundEnter = false;
    bool *isEnterFound = &foundEnter;
    Node *root = NULL; /* First node of the tree */
    int start = 0; /* Pointer to what was the last thing readed in text string */

    while (start < strlen(text)) {
        word = getWord(text, start, isEnterFound); /* If getWord detected enter foundEnter will become true */
        tolowerString(word);
        start += strlen(word) + 1;

        if (strcmp(word, "") != 0) /* If word is not NULL */
        {
            insert(&root,word,line);
        }

        if (foundEnter)
        {
            foundEnter = false;
            line++;
        }
    }

    /* Ptinting the tree */
    printTreeRec(root, outputFile);

    /* Freeing the tree */
    freeTree(root);
    fclose(inputFile);
    fclose(outputFile);
    free(ouputAddress);
    free(text);
    free(word);

    return 0;
}

Related Solutions

WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full...
WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full code
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
C program! Create a list of 5 things-to-do when you are bored (in the text file...
C program! Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50. Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character). After reading things in,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT