Slapshot136
Divide et impera
- Reaction score
- 471
Hi, i need some help checking the ending of a string, what would be the simplest way of doing that? (for a file type extension that may or may not be present)
#include <string>
#include <iostream>
int findSuffix(std::string &str, std::string &pattern)
{
int lstr = str.length();
int lpat = pattern.length();
if (lstr < lpat)
{
return 0;
}
return str.compare(lstr - lpat, lpat, pattern) == 0;
}
int main(int argv, char** argc)
{
std::string a = "filename.txt";
std::string b = "txt";
std::cout << findSuffix(a, b) << std::endl;
return 0;
}
cin>>name;
if (name == "*.htm" || name == "*.html" ) {
name = name;
} else {
name = name + ".html";
}
std::string getFileExt(const std::string& fname) {
size_t lastdot = fname.find_last_of('.');
return (lastdot != std::string::npos) ? fname.substr(lastdot+1) : "";
}
I want to check to see if there is a .htm or .html ending to a string, and if there is not, to add it.
cin >> name;
if (!findSuffix(name, ".htm") && !findSuffix(name, ".html"))
{
name = name + ".html";
}