Your error is with isalpha(a[i+1]!=0)
, as you should simply check isalpha(a[i+1])
. The return-value of the expression a[i+1]!=0
is 1 for all the characters in the input string, except for the last one. The return-value of isalpha(1)
is 0, and so the statement under the if
condition will execute only for the last character, and only if that character is preceded by a space character.
Having said that, here is a better way for you to implement the whole thing:
for (i=1; a[i]!=0; i++)
if (a[i-1]==' ' && isalpha(a[i]))
a[i] = toupper(a[i]);