How can I check whether a string ends with .csv
in C?
I've tried using strlen
without any success.
How about:
char *dot = strrchr(str, '.');
if (dot && !strcmp(dot, ".csv"))
/* ... */
The simplest (and most general) form of ThiefMaster's code would be:
int string_ends_with(const char * str, const char * suffix)
{
int str_len = strlen(str);
int suffix_len = strlen(suffix);
return
(str_len >= suffix_len) &&
(0 == strcmp(str + (str_len-suffix_len), suffix));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With