str[i] is ' ' when you enter this for loop. If you set j to i, then str[j] is ' ' and the for loop's condition is false on the first iteration so the body of the loop is never executed. You should probably set neww[k] to '\0' when the outer loop is done.