but I need to do it in a loop
Not from where I'm sitting. If you copy 4 characters from temp into buf, then if temp has "hello", buf will have "hell" after calling strncpy(). If you also want to remove those characters from temp, you need to shift them away after copying them. You can do that with memmove:
Code:
strncpy( buf, temp, 4 );
buf[4] = '\0';
memmove( temp, temp + 4, strlen( temp + 4 ) );
or with a loop:
Code:
strncpy( buf, temp, 4 );
buf[4] = '\0';
for ( c = 0; temp[c]; c++ ) {
temp[c] = temp[c + 1];
}
p.s. sorry to start a huge ordeal on scanf.
No biggie. It was a good debate until the other guy started showing his true colors.