Originally Posted by mina86
This is not valid C. You probably meant while (a[i]). The same applies for the rest of the code.
I'm sure the OP meant '\0'
instead of ''
. In a true/false test a[i]
is true/false the same as a[i]!='\0'
so your suggestion of removing the !=''
would work as well as the clearer alternative of add the \0
Since the code wouldn't even compile, the reported seg fault is hard to explain.
With correct input the program adjusted just enough to compile then works correctly and does not seg fault.
Rather than just tell someone never use gets() it is better to explain that when the input is longer than the buffer, gets() will corrupt memory with almost any result possible, including seg fault. Then the beginner writing the code can decide whether input longer than buffer is a condition he chooses to worry about.