Around 15:12 minutes later in video .Explain string rotation question with another example which is not discussed in tutorial . PLEASE EXPLAIN IN BRIEF .
String rotation question
Hello @Nikhil-Aggarwal-2320066674901389,
Let’s take another example:
-
Suppose, arr[100]=“NIKHIL”, K=2(Rotate string by 2 positions in left direction)
-
In the rotate function after executing the first loop, the string inside the array will become: “NINIKHIL”
Why?
i=6(length of the “NIKHIL”)
starting from the index, i+k-1=6+2-1=7; till (i > 0)
arr[i+k-1]=arr[i-1] i.e. arr[7]=arr[5] i.e. arr[7]=“L”
decrement i, i will become 4; and arr[i-k]=arr[6]=“I”
similarly,
for i=3 : arr[5]=“H”
for i=2 : arr[4]=“K”
for i=1 : arr[3]=“I”
for i=0 : arr[2]=“N”
arr[1]=“I” and arr[0]=“N” (as there is no modification at these indexes) -
After we execute the second while loop, string will become: “ILNIKHIL”
now, i has become 8 as two characters are added in the string
s=0, j=i-k=8-2=6, arr[0]=arr[6]=“I”
s=1, j=7, arr[1]=arr[7]=“L” -
arr[i-k]=arr[6]=’\0
Hope, this would help.
Give a like, if you are satisfied.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.