How to Compare Last n Characters of A String to Another String in C

后端 未结 6 1544
野性不改
野性不改 2021-01-02 07:41

Imagine that I have two strings, one of them is a url like \"/sdcard/test.avi\" and the other one is\"/sdcard/test.mkv\". I want to write an if statement that looks whether

6条回答
  •  攒了一身酷
    2021-01-02 08:19

    If you have a pointer-to-char array, str, then this:

    int len = strlen(str);
    const char *last_four = &str[len-4];
    

    will give you a pointer to the last four characters of the string. You can then use strcmp(). Note that you'll need to cope with the case where (len < 4), in which case the above won't be valid.

提交回复
热议问题