Use strrchr() insteaad of rindex().