Skip to content

Instantly share code, notes, and snippets.

@EncodeTheCode
Created January 12, 2025 20:34
Show Gist options
  • Save EncodeTheCode/8ecd78adff5aef5a07ba4a3f66fa69b0 to your computer and use it in GitHub Desktop.
Save EncodeTheCode/8ecd78adff5aef5a07ba4a3f66fa69b0 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
bool hasPngExtension(const std::string& filename) {
// Find the last occurrence of the dot
size_t dotPos = filename.rfind('.');
if (dotPos == std::string::npos) {
// No dot found, no extension
return false;
}
// Extract the extension
std::string extension = filename.substr(dotPos + 1);
// Check if the extension matches "png" strictly (case-sensitive)
return extension == "png";
}
int main() {
std::string filename1 = "image.png";
std::string filename2 = "image.PNG";
std::string filename3 = "document.pdf";
std::string filename4 = "no_extension";
std::cout << std::boolalpha; // Print boolean values as true/false
std::cout << "File: " << filename1 << ", Has .png: " << hasPngExtension(filename1) << '\n';
std::cout << "File: " << filename2 << ", Has .png: " << hasPngExtension(filename2) << '\n';
std::cout << "File: " << filename3 << ", Has .png: " << hasPngExtension(filename3) << '\n';
std::cout << "File: " << filename4 << ", Has .png: " << hasPngExtension(filename4) << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment