Please help me with understanding my mistake

if I run this code for s.length()-1 it gives error but it works fine for s.length(). this is for removing consecutive duplicates in a string. Kindly help me with this please

@aman212yadav please help