Question

I'm stuck with one problem in my little program in C and I really, really need your help. The 90% of the code works perfectly, because I checked multiple times, the problem is in the last lines and I can't figure out how to fix it. I'm still only a beginner so there's probably a really basic mistake somewhere.

My program puts all the words from txt file on a list [checked, works fine]. The user types the keyword. In the keyword instead some letters it can be used '?' meaning any random letter, '*' meaning any random amount of letters at the end and [x,y,z] meaning the letter could be x or y or z, then the program checks if any of words from the list match. The function that checks all these conditions works very good as well [I checked it on some random words], but somehow I fail when trying to make it check all the words from the list and printf the ones that match. I'd be very grateful if someone can help me fix this problem.

Here's the code:

typedef struct bazaslowek                                    // structure
    {
        char *wordlist;
        struct bazaslowek* next;
    } baza;

int MatchWord(char *Word, char *Sequence)    //function, here works everything

{int i = 0; 
int j = 0; 
int k = 0;
int LastChar = 0;
int CharMatch = 0;
char SpecifiedChars[20];

while(Word[i]!='\0' && Sequence[j]!='\0')
{if(isalpha(Sequence[j]))
    {if(Word[i]!=Sequence[j])
        {return 0;}
    i++;
    j++;
    }
if(Sequence[j] == '?')
    {i++;
    j++;}
if(Sequence[j] == '[')
    {j++;
    while(Sequence[j]!= ']')
        {if(isalpha(Sequence[j]))
            {SpecifiedChars[LastChar] = Sequence[j];
            LastChar++;
            j++;}
        else
            { j++;}
        }
    j++;
    for(k = 0 ; k <= LastChar ;k++)
        {if(SpecifiedChars[k]==Word[i])
            {CharMatch = 1;}
        SpecifiedChars[k] = ' ';}
        SpecifiedChars[0] = '\0';
        LastChar = 0;
        if(!CharMatch)
            {return 0;}
        i++;
    }
    if(Sequence[j] == '*')
    { j++;
    while(Word[i]!='\0')
        { i++;}
    }
}
return 1;
}





int main()


{    //============================== txt file -> list, works fine

baza *head = NULL;
char wordlist[30];
FILE *fp;
if ((fp = fopen("bazaslow.txt", "r"))==NULL)
    {printf("Error while opening!");
    exit(EXIT_FAILURE);}
else
    {
    while(!feof(fp))
        {
        fscanf(fp,"%s\n", wordlist);
        baza *wsk = head;
        baza *new = malloc (sizeof(baza));
        new -> next = NULL;
        new -> wordlist = strdup(wordlist); 
        if(wsk == NULL)
            {
            new -> next = head;
            head = new;
            }
        else
            {
            while(wsk -> next != NULL)
            wsk = wsk -> next;
            wsk -> next = new;
            }
        }
    }
fclose(fp);


//=============================HERE THE PROBLEM STARTS

char word[30], keyword[30];
printf("Type the keyword: \n");
scanf("%s", keyword);
baza *wskx = head;
while (wskx != NULL)
    {wskx->wordlist=word;
    printf("%s\n", wskx->wordlist);
    if(MatchWord(word,keyword))
        {printf("\n%s", wskx->wordlist);}
    wskx=wskx->next;}
return 0;
}

With this code it printfs me a lot of lines of some random letters and characters that are not even on list, every time they're different. I'm really, really in need of your help, I'm trying to figure out this problem for a long time already, but every time I fail.

Was it helpful?

Solution 2

It should be like this:

char keyword[30];
printf("Type the keyword: \n");
scanf("%s", keyword);
baza *wskx = head;
while (wskx != NULL)
{
    if(MatchWord(wskx->wordlist,keyword))
        printf("\n%s", wskx->wordlist);
    wskx=wskx->next;
}

OTHER TIPS

You are assignment is wrong. Instead of

wskx->wordlist=word;

you should do

word=wskx->wordlist;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top