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.
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment