I'm new to C and I i have an assignment where I have to build a dictionary (Linked List in a way). Basically the user inputs several words,year and their definition this:

Example:

love_#2004#LOVING
trade
#2001#_INVEST
etc...
And basically i need a function to scan the definition (Ex: INVEST) and gives me the word trade.

If the definition is related to more than only one word to give me back all the words it relates to.

What sort of a funtion do I need to scan these strings?

The standard C library function strstr can be used to search a string for a substring.

I'd start by breaking down the line into something more manageable:

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

typedef struct definition {
    char *key;
    char *datestamp;
    char *value;
} definition;

definition parse_definition(const char *s)
{
    char *copy = _strdup(s); // Make a copy for strtok
    char *tok = strtok(copy, "#");
    definition result = {0};

    if (tok != NULL) {
        result.key = _strdup(tok);
    }
    else {
        result.key = _strdup("");
    }

    tok = strtok(NULL, "#");

    if (tok != NULL) {
        result.datestamp = _strdup(tok);
    }
    else {
        result.datestamp = _strdup("");
    }

    tok = strtok(NULL, "#");

    if (tok != NULL) {
        result.value = _strdup(tok);
    }
    else {
        result.value = _strdup("");
    }

    free(copy); // Clean up temporaries

    return result;
}

int main(void)
{
    definition def = parse_definition("love_#2004#LOVING");

    printf("%s\n%s\n%s\n", def.key, def.datestamp, def.value);

    free(def.key);
    free(def.datestamp);
    free(def.value);

    return 0;
}

From there you can easily traverse the list of definitions and use strstr on value, then print key when there's a match.

how can I work with a struct like this?

struct node
{
    char data[100];
    struct node *previous;  // Points to the previous node
    struct node *next;   // Points out to the next node
}*head, *last;

will I need t add to my struct?

Once again, this is just me, but I'd merge the two like this:

struct node
{
    definition parsed_data;
    struct node *previous;  // Points to the previous node
    struct node *next;   // Points out to the next node
}*head, *last;

Or like this, if you want to retain the original string:

struct node
{
    char data[100];
    definition parsed_data;
    struct node *previous;  // Points to the previous node
    struct node *next;   // Points out to the next node
}*head, *last;

can you explain to me definition parsed_data?
what is it?

ohh you mean it's the node

struct node
{
    char data[100];
    node parsed_data;
    struct node *previous;  // Points to the previous node
    struct node *next;   // Points out to the next node
}*head, *last;

Like this if i'm not mistaken

can you explain to me definition parsed_data?
what is it?

Read the code from my first post.

yeah got it thanks will exercise it

hey there thanks for the posts.
my main problem is that the user will input everything in one sentence but not seperately.

How do I work with that?

Can you point out places where you want to split the sentence? That's where you start, and figure out how to do it programmatically. Conveniently enough, my example showed how to use strtok, which is C's standard tokenization function. ;)

Okay since every word is seperated by "_ # _" i think i have to point to each character. I'm having trouble explaining it let me know if you understand what i'm searching for.

Okay so we have for example:

trade_#_ 2003 _#_INVEST

This is what was inputed.
My function asks what definition do i want to search?
Input: INVEST
Output: found INVEST in word: trade

secondly i wanted to say thank you so much for your help.

by the way i'm building a linked list

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.