一道笔试题的解法

题目是:

程序为:

#include<iostream>
#include
<string>
using namespace std;

string format(string macstr)
{
string formatedstr = "";
for(int i = 0; i < macstr.size(); i++)
{
char c = macstr[i];
if((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9'))
{
formatedstr.append(
1,c);
}
}
for(int j = 2; j < formatedstr.size(); j += 3)
{

formatedstr.insert(j,
1,':');

}
return formatedstr;
};

int main()
{
string s = "00:50-8d-49-db";
cout
<< "formated: " << format(s) << endl;

s
= "00:508d-49-db";
cout
<< "formated: " << format(s) << endl;

return 1;
}



posted @ 2011-09-20 16:13  yanng  阅读(849)  评论(0编辑  收藏  举报