z1
is pointing to a string literal and modifying a string literal is undefined behavior. Alternatively you can use the following declaration of z1
which will be modifiable:
char z1[] = " weeee";
If we look at the C99 draft standard section 6.4.5
String literals paragraph 6 says(emphasis mine):
It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
A few other points as WhozCraig points out this line:
while (*p == ' ' && *p != '\0') {
could be more succinctly written as:
while (*p == ' ' ) {
also you are using indirection here:
*p++;
but you don't actually use the resulting value, so you can change it to:
p++;