Text Analysis/assign3.c
void wordAnalysis(char **bufferPtr, int numOfLines){
    puts("");
    
    //used for printing results
    const char *singleWord = "time";
    const char *pluralWord = "times";

    //makes a copy of the original data to avoid data destruction
    char copy[MAX_LINES * MAX_LINE_LENGTH];
    make1DArrayFrom2D(copy, bufferPtr, numOfLines);
        
    //creates a second copy of the data because strtok modifies its data being tokenized
    char copy2[MAX_LINES * MAX_LINE_LENGTH];
    strcpy(copy2, copy); 
    
    char *strTokenPtr;
    int wordCount = 0;
    
    //tokenizes the 1D string to count the words
    strTokenPtr = strtok(copy2, " \r\n"); 
    while(strTokenPtr != NULL){
        wordCount++;
        strTokenPtr = strtok(NULL, " \r\n");
    }
    
    //re-copies the data to re-use variable and use unmodified data
    strcpy(copy2, copy); 
    
    //create an array of size word count to store the tokenized words
    char wordArray2D[wordCount][MAX_WORD_LENGTH];
    
    //tokenizes and stores each word in a 2D word array
    strTokenPtr = strtok(copy2, " \r\n"); 
    int count = 0;
    while(strTokenPtr != NULL){
        strcpy(wordArray2D[count++], strTokenPtr);
        strTokenPtr = strtok(NULL, " \r\n");
    }
    
    strcpy(copy2, copy); 
        
    //looks for and compares words for recurring occurrences in the 2D array
    int occurrences = 0;
    
    for(int i = 0; i < wordCount; i++){ 
    
        if(wordArray2D[i][0] != '\0'){  //check if our word is NULL or was deleted
        
            printf("\"%s\" ",  wordArray2D[i]); //prints the word we are counting
            
            for(int j = 0; j < wordCount; j++){
                if(!strcmp(wordArray2D[i], wordArray2D[j])){
                    //if we counted the word already, we can count and remove(by setting the location to 0) subsequent occurrences
                    if(occurrences >= 1){
                        memset(wordArray2D[j], '\0', sizeof(wordArray2D[j]));
                    }
                    occurrences++;
                }              
            }         
             
            if(occurrences <= 1){
                printf("%*s %d %s\n", MAX_WORD_LENGTH - (int) strlen(wordArray2D[i]), "appeared", occurrences, singleWord);
            }
            else{
                printf("%*s %d %s\n", MAX_WORD_LENGTH - (int) strlen(wordArray2D[i]), "appeared", occurrences, pluralWord);
            }
            occurrences = 0;
        }
    }    
}
/*
    wordAnalysis: analyses the repetition of words in a given text and prints the results
    input: an array of text (bufferPtr), the number of lines to be analysed (numOfLines)
*/
analyses the repetition of words in a given text and prints the results
input: an array of text (bufferPtr), the number of lines to be analysed (numOfLines)
analyses the repetition of words in a given text and prints the results input : an array of text ( bufferPtr ) the number of lines to be analysed ( numOfLines )
void wordAnalysis(char **bufferPtr, int numOfLines){
    puts("");
    const char *singleWord = "time";
    const char *pluralWord = "times";
    char copy[MAX_LINES * MAX_LINE_LENGTH];
    make1DArrayFrom2D(copy, bufferPtr, numOfLines);
    char copy2[MAX_LINES * MAX_LINE_LENGTH];
    strcpy(copy2, copy); 
    char *strTokenPtr;
    int wordCount = 0;
    strTokenPtr = strtok(copy2, " \r\n"); 
    while(strTokenPtr != NULL){
        wordCount++;
        strTokenPtr = strtok(NULL, " \r\n");
    }
    strcpy(copy2, copy); 
    char wordArray2D[wordCount][MAX_WORD_LENGTH];
    strTokenPtr = strtok(copy2, " \r\n"); 
    int count = 0;
    while(strTokenPtr != NULL){
        strcpy(wordArray2D[count++], strTokenPtr);
        strTokenPtr = strtok(NULL, " \r\n");
    }
    strcpy(copy2, copy); 
    int occurrences = 0;
    for(int i = 0; i < wordCount; i++){ 
        if(wordArray2D[i][0] != '\0'){  
            printf("\"%s\" ",  wordArray2D[i]); 
            for(int j = 0; j < wordCount; j++){
                if(!strcmp(wordArray2D[i], wordArray2D[j])){
                    if(occurrences >= 1){
                        memset(wordArray2D[j], '\0', sizeof(wordArray2D[j]));
                    }
                    occurrences++;
                }              
            }         
            if(occurrences <= 1){
                printf("%*s %d %s\n", MAX_WORD_LENGTH - (int) strlen(wordArray2D[i]), "appeared", occurrences, singleWord);
            }
            else{
                printf("%*s %d %s\n", MAX_WORD_LENGTH - (int) strlen(wordArray2D[i]), "appeared", occurrences, pluralWord);
            }
            occurrences = 0;
        }
    }    
}
void wordAnalysis ( char * * bufferPtr , int numOfLines ) { puts ( " " ) ; const char * singleWord = " " ; const char * pluralWord = " " ; char copy [ MAX_LINES * MAX_LINE_LENGTH ] ; make1DArrayFrom2D ( copy , bufferPtr , numOfLines ) ; char copy2 [ MAX_LINES * MAX_LINE_LENGTH ] ; strcpy ( copy2 , copy ) ; char * strTokenPtr ; int wordCount = 0 ; strTokenPtr = strtok ( copy2 , " \r \n " ) ; while ( strTokenPtr != NULL ) { wordCount ++ ; strTokenPtr = strtok ( NULL , " \r \n " ) ; } strcpy ( copy2 , copy ) ; char wordArray2D [ wordCount ] [ MAX_WORD_LENGTH ] ; strTokenPtr = strtok ( copy2 , " \r \n " ) ; int count = 0 ; while ( strTokenPtr != NULL ) { strcpy ( wordArray2D [ count ++ ] , strTokenPtr ) ; strTokenPtr = strtok ( NULL , " \r \n " ) ; } strcpy ( copy2 , copy ) ; int occurrences = 0 ; for ( int i = 0 ; i < wordCount ; i ++ ) { if ( wordArray2D [ i ] [ 0 ] != ' \0 ' ) { printf ( " \" \" " , wordArray2D [ i ] ) ; for ( int j = 0 ; j < wordCount ; j ++ ) { if ( ! strcmp ( wordArray2D [ i ] , wordArray2D [ j ] ) ) { if ( occurrences >= 1 ) { memset ( wordArray2D [ j ] , ' \0 ' , sizeof ( wordArray2D [ j ] ) ) ; } occurrences ++ ; } } if ( occurrences <= 1 ) { printf ( " \n " , MAX_WORD_LENGTH - ( int ) strlen ( wordArray2D [ i ] ) , " " , occurrences , singleWord ) ; } else { printf ( " \n " , MAX_WORD_LENGTH - ( int ) strlen ( wordArray2D [ i ] ) , " " , occurrences , pluralWord ) ; } occurrences = 0 ; } } }
wordAnalysis: analyses the repetition of words in a given text and prints the results
input: an array of text (bufferPtr), the number of lines to be analysed (numOfLines)
wordAnalysis : analyses the repetition of words in a given text and prints the results input : an array of text ( bufferPtr ) the number of lines to be analysed ( numOfLines )
//used for printing results //makes a copy of the original data to avoid data destruction //creates a second copy of the data because strtok modifies its data being tokenized //tokenizes the 1D string to count the words //re-copies the data to re-use variable and use unmodified data //create an array of size word count to store the tokenized words //tokenizes and stores each word in a 2D word array //looks for and compares words for recurring occurrences in the 2D array //check if our word is NULL or was deleted //prints the word we are counting //if we counted the word already, we can count and remove(by setting the location to 0) subsequent occurrences