# Thread: manipulation of strings using pointer notation

1. ## 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');

}

2. What is it exactly that you want to fix? The problem with spaces?

3. If so, just do an extra iteration to create the space or append it on to the char *. Toggle with that.

4. Originally posted by Garfield
What is it exactly that you want to fix? The problem with spaces?
Well, I'd like to fix that yes, But there is something that I am missing in my thinking...period. I've been looking at this simple function for 6 hours now and trying different things.
Grrrr

So if you can shed some light on the art of inserting characters in this manner it might help me figure it out on my own even.

This function should be able to insert a string anywhere in the source. Either at the beginning, middle or end of the string. It should be intelligent enough to know to leave room for a space...or not.

Thanks!!

Mike

5. Originally posted by Garfield
If so, just do an extra iteration to create the space or append it on to the char *. Toggle with that.
That was the first thing I did try to make the extra space at the end of the string. then to move each item over that many spaces...
*p= source+inserted+1 //+1 for the added space.

Then I did
*p = *(p-length) where length is the length of the string to insert.

that causes other kinds of weirdness such as overwriting parts of the source string.

and thanks for responding!!

Mike

6. Mmmm
Code:
```#include <stdio.h>
#include <string.h>

// insert s2 into s1 at position pos
void insertstring ( char *s1, char *s2, int pos ) {
int len1 = strlen(s1);
int len2 = strlen(s2);
int i;

// relocate the tail of s1
// len1-pos chars need to be moved up len2 places
// starting at position pos
for ( i = len1 - pos ; i >= 0 ; i-- ) {
s1[pos+len2+i] = s1[pos+i];
}

// copy s2 into the hole
for ( i = 0 ; i < len2 ; i++ ) {
s1[pos+i] = s2[i];
}

// restore the end of string
s1[len1+len2] = '\0';
}

int main ( ) {
char buff[100] = "hello world";
insertstring( buff, "abc ", 6 );
printf( "%s\n", buff );
insertstring( buff, "foo ", 0 );
printf( "%s\n", buff );
return 0;
}```
Shouldn't be too hard converting it to use pointer notation....

7. Salem,
thanks a gig! a tera even!

This helped me tremendously. It not only did that, but it taught me something really good.

Didn't some guy named Stee moderate this about a year ago?
I am just wondering what happened to him. That's all.
or...are you one in the same?

Thanks agian,
Mike

8. > Didn't some guy named Stee moderate this about a year ago?
He did

> are you one in the same?
Yes

Popular pages Recent additions