0

My code is supposed to read file and store its contents in a linked list. Then, search for the user input word and print its location. Here is my try on it. I am kind of confused with the strcmp as well. Another thing i am not sure about is how to print the node (index) in the linked list where the word is found.

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

struct list {
    char *string;
    struct list *next;
};

typedef struct list LIST;

int main() {
    FILE *fp;
    char line[128];
    LIST *current, *head;

    head = current = NULL;
    fp = fopen("words.dat", "r");

    while(fgets(line, sizeof(line), fp)){
        LIST *node =(LIST*) malloc(sizeof(LIST));
        node->string = strdup(line);//note : strdup is not standard function
        node->next =NULL;

        if(head == NULL){
            current = head = node;
        } else {
            current = current->next = node;
        }
    }


    
    
    fclose(fp);
    //test print
    for(current = head; current ; current=current->next){
        printf("%s", current->string);
    }
    //searching for a word
    char string[50];
    printf("\nEnter the word to be searched\n");
    scanf("%s",&string);
    LIST *tp1;
    int flag;
    tp1 = head;
    while(tp1 != NULL)
    {

        if(strcmp(tp1->string,string)==0)
            {
                flag = 0;
                printf("%d",(tp1->string)) ;
                break;
            }
        tp1 = tp1->next;
    }
    if (flag)
        printf("\nThe name to be searched is not in the linked list\n");
    else
        printf("\nThe name to be searched is in the linked list\n");    

    return 0;    
}
  
paddy
  • 60,864
  • 6
  • 61
  • 103
Infinitypool
  • 1
  • 1
  • 2
  • What exact problem are you having? You have only described what you're trying to do, and posted a program. The list construction looks okay, except that you must understand `fgets` will give you a string that includes the newline (if any). You must remove the newline from the end of the string. Otherwise your string comparisons will always fail because the `scanf` call does _not_ give you a string with newlines. This code is _NOT_ C++ by the way. Best not to use the C++ tag. – paddy Apr 23 '21 at 03:12
  • Regarding printing the index in the list when you find the string, currently you have `printf("%d",(tp1->string))` which is outright wrong. You cannot treat a `char*` as a `int`. That has nothing to do with an index. Simply set `int index = 0;` before your loop, and `++index;` every time you advance to the next list node. This is not rocket science. `printf("%d\n", index);` will then display the index. – paddy Apr 23 '21 at 03:14
  • Oh thats just an error i posted while trying to work it out. – Infinitypool Apr 23 '21 at 03:15
  • 1
    Your code looks okay apart from the issues I pointed out in my first two comments. Read them carefully and address them, then test your program. – paddy Apr 23 '21 at 03:16
  • When i try to search for a word, the strcmp just doesnt work. By logic. I am entering a word which is supposed to be at one of those nodes. But it ends returning false. – Infinitypool Apr 23 '21 at 03:16
  • 1
    Okay, read my third comment then, please. – paddy Apr 23 '21 at 03:17
  • 1
    A further issue: `scanf("%s",&string);` should be `scanf("%s", string);` – paddy Apr 23 '21 at 03:18
  • Thanks a lot. I have figured out the problem. My code read 1 entire string instead of word-by-word. Could you tell me how to read the file word by word instead of using fgets. Pretty sure that is the problem. – Infinitypool Apr 23 '21 at 03:21
  • You can use `fscanf`, or you can read the line and split it into words with `strtok`. – paddy Apr 23 '21 at 03:22

0 Answers0