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

后端 未结 6 1522
野性不改
野性不改 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:34

    In pure C you can only resort to manual compare:

    int endswith(const char* withwhat, const char* what)
    {
        int l1 = strlen(withwhat);
        int l2 = strlen(what);
        if (l1 > l2)
            return 0;
    
        return strcmp(withwhat, what + (l2 - l1)) == 0;
    }
    

提交回复
热议问题