Why do you have a bunch of variable names with leading underscores? That's nasty.
Anyway, the first thing you must do is check the return value of fgets
. If it returns NULL, you didn't get any input. (You can then test feof
or ferror
to find out why you didn't get input.)
Moving on to cleanString
, you have a while loop that consumes a sequence of non-printable characters (and you could use isprint
for that instead of magic numbers), followed by a while loop that consumes a sequence of printable characters. If the input string doesn't consist of a sequence of non-printables followed by a sequence of printables, you will either consume too much or not enough. Why not use a single loop?
while(str[i]) {
if(isprint(str[i]))
temp[j++] = str[i];
++i;
}
This is guaranteed to consume the whole string until the \0
terminator, and it can't keep going past the terminator, and it copies the "good" characters to temp. I assume that's what you wanted.
You don't even really need to use a temp buffer, you could just copy from str[i]
to str[j]
, since j
can never get ahead of i
you'll never be overwriting anything that you haven't already processed.