#include <stdio.h>
#include <string.h>
void tokenize_file(const char *filename);

/*
    * strtok is a function in C that is used to tokenize a string.
    * It splits a string into smaller strings (tokens) based on specified delimiters.
    * The first call to strtok should pass the string to be tokenized,
    * and subsequent calls just pass NULL to continue tokenizing the same string.
    * The function modifies the original string by replacing delimiters with null characters.
    * It returns a pointer to the first token found in the string.
    * If no tokens are found, it returns NULL (this means it has reached the end of the string).
*/
int main() {
    // Input string to be tokenized
    // char str[] = "CS262 is a fun course to learn programming";
    // Demo with other punctuation marks
    char str[] = "CS262,is a fun course!to\nlearn\nprogramming.";
    // char str[] = "CS262: is a fun course; to learn programming?";
    
    // Delimiters to split the string (space in this case)
    // const char delim[] = " ";
    // If we wanted multiple delimiters, we could use something like:
    // const char delim[] = " ,.!?;:\n"; // This includes space and punctuation marks
    const char delim[] = " ,\n";
    // Pointer to hold each token
    char *token;

    // Print the original string
    printf("Original string: \"%s\"\n", str);

    // Use strtok to get the first token
    token = strtok(str, delim);

    // Loop through the string to extract all tokens
    while (token != NULL) {
        // Print each token
        printf("Token: \"%s\"\n", token);

        // Get the next token
        token = strtok(NULL, delim);
    }

    tokenize_file("file.txt");

    return 0;
}

// Function to use strtok to tokenize a newline delimited file
void tokenize_file(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }



    char line[256]; // Buffer to hold each line
    while (fgets(line, sizeof(line), file)) {
        // Remove newline character from the end of the line
        // Tokenize the line using strtok

        char *token = strtok(line, "\n");
        while (token != NULL) {
            printf("%s ", token);
            token = strtok(NULL, "\n");
        }
    }
    fclose(file);
}
