strlen komutu ile kaç karakter olduğunu öğrenip dönğüyü de ona göre yapabilirsin.
int ksayi = strlen(str);
for (int i = 0; i<=ksayi; i++)

gibi.