How can I check if a string ends with ".csv" in C

How to check if a string ends with ".csv" in C

I tried using strlen without any success

+6
c string
Apr 27 2018-12-12T00:
source share
3 answers

What about:

 char *dot = strrchr(str, '.'); if (dot && !strcmp(dot, ".csv")) /* ... */ 
+34
Apr 27 '12 at 8:57
source share
 if(strlen(str) > 4 && !strcmp(str + strlen(str) - 4, ".csv")) 
+25
Apr 27 '12 at 9:00
source share

The simplest (and most common) form of ThiefMaster 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)); } 
+4
May 12 '16 at 13:40
source share



All Articles