Page 98, Exercise 6 :Rewrite Program 2.12 without temp
void strnins(char *s, char *t, int i)
{/* revised version of program 2.12 */
char string[MAX_SIZE];
if( (i < 0) || (i > strlen(s)))
printf("Position is out of bounds \n");
else {
if (!(strlen(s)))
strcpy(s,t);
else if (strlen(t)) {
strncpy(string, s,i);
string[i]='\0';
printf("After strncpy: %s\n", string);
strcat(string,t); printf("After strcat: %s\n", string);
strcat(string, (s+i));
strcpy(s, string);
}
}
} |