mercredi 1 septembre 2021

whats the exact rule in function declaration in c++

I want to know is it mandatory to have constant arguments in the functions. Recently I had in interview and wrote a code like below

int numofsubstring(string s1, string s2)
{
   int nCount =0;
   size_t pos;
   while((pos = s1.find(s2)) != string::npos)
   {
     nCount++;
     s1 = s1.substr(s1.find(s2)+s2.length());
   }
    return nCount;
}  //number of times s2 is present in s1

The panel was expecting const string&s1 ,const string&s2 but i didn't wrote bcz of substr functionality usage...

can any one guide what's the standardised format to write the code.... the panel was insisting to improve the arguments in the function.

Aucun commentaire:

Enregistrer un commentaire