Traverse from right until we find a digit which is greater than its next digit . For example , in 34125 , our index is 4.
for ( $ i = $ len - 2 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] > $ str [ $ i + 1 ] ) { $ index = $ i ; break ; } }