Requires time to be an O (n) complexity for string operations of length n and auxiliary memory O (1)
char* leftrotatestring (char* pStr, int n)
{
if (pStr! = NULL)
{
int nlength = static_cast<int> (strlen (PSTR));
if (nlength > 0 && n > 0 && N < nlength)
{
char* pfirststart = pStr;
char* pfirstend = pStr + n-1;
char* psecondstart= pStr + N;
char* psecondend = pStr + nLength-1;
//Flip the front n characters of a string
Reverse (Pfirststart, pfirstend);
//Flip the back part of the string
Reverse (Psecondstart, psecondend);
//Flip Entire string
Reverse (Pfirststart, psecondend);
}
}
return pStr;
}
void Reverse (char* pbegin, char* pEnd)
{
if (Pbegin = = NULL | | pEnd = = NULL)
return;
While (Pbegin < pEnd)
{
char temp = *pbegin;
*pbegin = *pend;
*pend = temp;
pbegin++, pend--;
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Left rotation string