Created
January 12, 2025 20:34
-
-
Save EncodeTheCode/e7f4c178cfc5ef1ce57687044c4d5821 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <algorithm> // For std::transform | |
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); | |
// Convert the extension to lowercase for case-insensitive comparison | |
std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower); | |
// Compare with "png" | |
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