Skip to content

Instantly share code, notes, and snippets.

@EncodeTheCode
Created January 12, 2025 20:36
Show Gist options
  • Save EncodeTheCode/8a1ad4eec0c6dd5bec8e83a2cf45ecfc to your computer and use it in GitHub Desktop.
Save EncodeTheCode/8a1ad4eec0c6dd5bec8e83a2cf45ecfc to your computer and use it in GitHub Desktop.

Revisions

  1. EncodeTheCode created this gist Jan 12, 2025.
    20 changes: 20 additions & 0 deletions file_extension_opt.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #include <iostream>
    #include <string>
    #include <algorithm>

    bool hasPngExtension(const std::string& filename) {
    size_t dotPos = filename.rfind('.');
    if (dotPos == std::string::npos) return false;
    std::string extension = filename.substr(dotPos + 1);
    std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
    return extension == "png";
    }

    int main() {
    std::cout << std::boolalpha
    << "File: image.png, Has .png: " << hasPngExtension("image.png") << '\n'
    << "File: image.PNG, Has .png: " << hasPngExtension("image.PNG") << '\n'
    << "File: document.pdf, Has .png: " << hasPngExtension("document.pdf") << '\n'
    << "File: no_extension, Has .png: " << hasPngExtension("no_extension") << '\n';
    return 0;
    }