string的妙用:
代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
string filename, basename, extname, tmpname;
const string suffix("tmp");
cin >> filename ;
size_t index = filename.find(".");
if (index == string::npos)
{
filename += "." + suffix;
}
else
{
basename = filename.substr(0,index+1);
extname = filename.substr(index+1);
if (extname.empty())
{
filename += suffix;
}
else
{
if (extname == "dat")
{
filename = basename + suffix;
}
if (extname == "tmp")
{
filename = basename + "xxx";
}
}
}
cout << filename << endl;
}