0

i am doing an exercise in C for my C programming course. I have to read data from a text file into a linked list and look for matches, then print the result out.

Example of the text file:

"Apple/Orange",1
"Banana/Watermelon/Lemon",2
"Watermelon/Strawberry",3
"Orange/Grape/Watermelon",4
"Blueberry", 5

Stored them into my linked list by using fgets(), sscanf() and a void function, therefore the string will be starting with a quotation mark.

The problem is when i tried to use strncmp() to find a word from the string, it didn't work due to the quotation mark.

I did something like:

void findFruits(List *list){

Node *position = list->first;
while(position != NULL){
    if(strncmp(position->fruits, "Watermelon", 10)==0){
        printf("%s, %d\n", position->fruits, position->number); 
    }
    position = position->next;
}

I literally have no clue for finding an exact word from the string which is beginning with a quotation mark, any help would be appreciated, thanks.

Solved now, thanks to Barmar's idea. It worked perfectly when i tried to use strstr() instead of strncmp().

if(strstr(position->fruits, "Watermelon")){
    printf("%s, %d\n", position->fruits, position->number); 
    }
Cyrus Leung
  • 106
  • 5
  • `(strncmp(position->fruits, "\"Watermelon\"", 10)`. That is, escape the double quote characters. Escaping is the standard way of specifying special characters in a string literal. – kaylum Nov 03 '16 at 19:50
  • Possible duplicate of [Include double-quote (") in C-string](http://stackoverflow.com/questions/20458489/include-double-quote-in-c-string) – kaylum Nov 03 '16 at 19:51
  • Alternatively, strip the quotation marks from the data before storing it in your linked list. That's a good idea if the quotation marks are most properly considered to be part of the data formatting, as opposed to being an integral part of the data. – John Bollinger Nov 03 '16 at 19:55
  • If the string is `Banana/Watermelon/Lemon` then `strncmp()` will not match it, because it starts matching at the beginning of the string. If you want to search for `Watermelon` anywhere in the string, you need to use `strstr()`. – Barmar Nov 03 '16 at 19:57
  • If you are supposed to extract the name of each fruit, and see if it has already been given, you could apply the `strtok` function to each line using the delimiter set `"/\""` which looks odd, but contains two delimiter characters `/` and `"` (the second is "escaped" because it is in a string which also requires the `"`). – Weather Vane Nov 03 '16 at 19:59
  • @kaylum Thanks, tried you method but it didn't work, i made it by using strstr(). – Cyrus Leung Nov 03 '16 at 20:28
  • @Barmar Thank you so much, i have been struggling for 2 hours on this problem and you answer works perfectly!! – Cyrus Leung Nov 03 '16 at 20:31

1 Answers1

0

if you want search a single fruit word, for instance Watermelon in a string Banana/Watermelon/Lemon, you can't compare those two string but you must split your string with this separator / and compare the word between two separator; or you can compare a single character of your fruit with your string.

F.Guerinoni
  • 277
  • 2
  • 12