corrected end index of string before while loop