>>Sure it can.
If domains (I'm assuming that's what '.com' etc. are called) are exclusive from right to left, then it can. If this is not the case, however, then:
www.abcdefg.hij
www.abcdefgh.ij
If you are given www.abcdefghij, then you will find two different possible domains. On the other hand, if it is guaranteed that domains will not overlap from right to left (i.e. [.com and .comp] are allowed but [.com and .acom] are not), then you can iterate the string backwards eliminating all domains that do not match until you have one string remaining. The same cannot be said if domains are only guaranteed not to overlap from left to right.
Code:
list<string> domainList;
//dottify() returns true if a unique match was found and the URL has been dotted.
bool dottify(string& s)
{
list<string> potentialDomains = domainList;
//Iterate backwards, checking each character with the domains.
for(string::size_type i = 1; i <= s.size(); ++i)
{
list<string>::iterator it = potentialDomains.begin();
//Check the corresponding character in each domain
while(it != potentialDomains.end())
{
if(it->size() < i) //Domain is too short
{
it = potentialDomains.erase(it);
continue;
}
if((*it)[it->size() - i] != s[s.size() - i]) //Character doesn't match
{
it = potentialDomains.erase(it);
continue;
}
++it;
}
if(potentialDomains.size() == 1) //If only 1 domain matches, see if it works.
{
if(potentialDomains.front().size() > s.size()) //Domain is too long
return false;
s.insert(s.size() - potentialDomains.front().size(), 1, '.');
return true;
}
else if(potentialDomains.size() == 0)
return false;
}
return false;
}
Note that this is untested, and also that I'm working off the assumption that domains are right-to-left exclusive (gee, I'm making up a lot of words lately..).