Search code examples
cstringnewlinestrcmp

strcmp with string ending with newline


I'm trying to write a C list filtering function that receives a country string, traverses the list and deletes all nodes with node->country == country. The problem is that node->country strings end with '\n' (because it's being read from a csv file) and therefore strcmp(node->country, country) never equals zero.

How would I solve this? I first thought about appending '\n' to country but that would probably raise more memory problems. Also thought about strstr but I don't really know how to work with that.

Thanks for any suggestions.


Solution

  • As you are using strcmp, let's assume some C-style code:

    node->country[strcspn(node->country, "\n")] = '\0';
    

    This will alter your node->country value and terminate your string right before a new line (if any).