manipulation of strings using pointer notation
in the following function,
s1 is a source string. such as
" for all and all for *1" taking note that the first character of this string is a space.
s2 is the string that we wish to insert into s1.
int n, is the position that we wish to insert s2.
What I have works. It creates the string " onefor all and all for *1"
how do I fix this?
just a note: the "else" in this function has not been tested. I don't know what it will do yet. I've not gotten that far.
insertstring(char *s1,char *s2, int n)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int i;
char *p2 = s2;
char *p1=s1+len1;
//make a hole for to insert the string
if(n > 0 )//if inserting between two chars in a string
{
for(i=0; i<(len1-n); ++i,--p1)
{
*p1=*(p1-len2);
}
}
else //insert at the beginning of a string.
for(i=0; i<=(len1-n); ++i,--p1) //if at the head of the sentence
{
*p1=*(p1-len2);
}
//insert the new string
do{
*p1 = *p2;
if(*p2 != '\0') ++p1; ++p2;
}
while(*p2 != '\0');
}