This
if(string[i] = ' ')
doesn't do what you think it does. With a single equals sign (assignment operator) you're saying you want the character at index i to be ' ' (0x20). What you want is the comparison operator '==', is the character at index i equal to ' '.
if(string[i] == ' ')