Microsoft Interview Question
Developer Program EngineersTeam: teamviewer
Country: India
Interview Type: In-Person
string shiftLeft(const string &lhs, int shiftNum) {
if (shiftNum == 0) {
return lhs;
}
if (shiftNum < 0) {
return shiftRight(lhs, -shiftNum);
}
if (shiftNum >= lhs.length()) {
shiftNum = shiftNum - shiftNum / lhs.length();
}
string result = "";
for (int i = (int)(lhs.length() - 1 - shiftNum); i < lhs.length(); i++) {
result += lhs[i];
}
for (int i = 0; i < (lhs.length() - 1 - shiftNum); i++)
{
result += lhs[i];
}
return result;
}
string shiftRight(const string &lhs, int shiftNum) {
if (shiftNum == 0) {
return lhs;
}
if (shiftNum < 0) {
return shiftLeft(lhs, -shiftNum);
}
if (shiftNum >= lhs.length()) {
shiftNum = shiftNum - shiftNum / lhs.length();
}
string result = "";
result += lhs.substr(shiftNum, lhs.length() - shiftNum);
result += lhs.substr(0, shiftNum);
return result;
}
class strshift
- suwei19870312 June 12, 2014{
string mStr;
public:
strshift()
{
}
strshift(char* istr):mStr(istr)
{
}
string operator << (int n)
{
n = n% mStr.length();
if(n == 0)
return mStr;
return (mStr + mStr).substr(mStr.length() -n, mStr.length());
}
};