Skip to content

Instantly share code, notes, and snippets.

@EncodeTheCode
Last active January 12, 2025 20:38
Show Gist options
  • Save EncodeTheCode/0a174b39d2fff11e6db834bf032e01b8 to your computer and use it in GitHub Desktop.
Save EncodeTheCode/0a174b39d2fff11e6db834bf032e01b8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <algorithm>
bool hasPngExtension(const std::string& filename) {
std::string extension = ".png";
if (filename.size() >= extension.size()) {
// Extract the last part of the string
std::string fileExtension = filename.substr(filename.size() - extension.size());
// Convert both to lowercase for case-insensitive comparison
std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(), ::tolower);
std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
return fileExtension == extension;
}
return false;
}
int main() {
// This is the filename with the file extension, change it to anything to compare and check with.
std::string filename = "example.PNG";
if (hasPngExtension(filename)) {
std::cout << "The file has a .png extension.\n";
} else {
std::cout << "The file does not have a .png extension.\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment