• C

search and replace

I am a total novice to C and am attempting to write a program using pointers and a function such as
int Globalreplace(char *string, char *oldstr, char *newstr)
the program accepts 3 strings eg
1 - globalreplace
2 - a
3 - wow

it prints out

it also returns a 0 or 1 to main ie if
2 appears in 1 or not

Please assist me in how to go about it
ie alogarithim, syntax, pasing arguments to the function etc especially with the pointers as they are really confusing me.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Here is a sketch of a simple algorithm to use:

for each character in string
   compare oldstr to string starting at the current character
   if equal
      set flag to report back
      copy oldstr to newstr
   else copy current character to newstr

initialize two pointers to the start of string, and newstr (e.g. char *s,*ns)

for compare you would use strncmp

to copy oldstr to newstr:
ns+=strlen(oldstr); /* advance ns to end */

to copy current character:

I could give a completed function, however you will learn much more if you attempt to write it yourself. I will be happy to continue to provide help as you go through this.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The assumption is made that the string 'a' will never exceed 1024 in length, even after the replacement.
If there is the chance that this will happen, increase the size of MAX.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define  MAX  1024

int Globalreplace(char *string, char *oldstr, char *newstr)
char* p;
int found = 0;

while ( (p = strstr(string, oldstr)) != NULL )
    found = 1;
    memmove( p + strlen(newstr), p + 1, strlen(p+1) +1 );
    strncpy(p, newstr, strlen(newstr));

return found;

int main()
char a[MAX + 1];
char b[] = "a";
char c[] = "wow";
int ret;

strcpy(a, "globalreplace");

ret = Globalreplace(a, b, c);

if ( ret == 1 )
    printf("The string changed!\n");
    printf("The string did not change\n");

printf("%s\n", a);

return 0;
cebitAuthor Commented:
Thanks for your comments, I will work
on your alogarithim and attempt to understand it fully.
I still have a block though as how to initialize the pointers and pass them from main to function and back.
I know what a pointer does but I have a problem understanding when to use them
to their fullest advantage.

Plse reply
Once again thank you very much
One reason for using pointers is that a certain amount of efficiency can be gained. Pointers are often associated with arrays of information. Accessing this through array mechanisms engenders addressing calculations (e.g. start of the array + index * element size). A pointer dispenses with most of that.

Initializing a pointer is relatively simple. For a particular variable you can use the address of operator:

int i,*p;

For an array, the variable name is already an address so:

int i[20],*p;

Passing a pointer into a function is trivial; just do it. Passing things back out of functions can have tricks associated with them since everything in C is pass by value. This means that the variables being passed in are COPIED onto the stack. The function thus gets access to the variables value, but cannot affect it. If the function changes the variable, the original is not affected, since the function is dealing with a COPY. So the simple straightforward way of getting things back out of a function is with the return statement. And this is all that is needed in your example; a return of 1 or 0 to indicate what activity went on.
However pointers can be involved if you want complicated or multiple returns from a function. In this case the canonical solution in C is to use pointers. Instead of passing the variables themselves into a function, you pass in pointers to the variables. Now the function can still not affect the pointers (since they are passed by value and are copies). But, the pointer that the function got, points to the ACTUAL memory the variable is stored in. So if the function stores something in the place that the pointer points to, the calling function will also have it changed (since it refers to that same memory). Example:

int i;

int *i;
{  *i=5;

the printf will produce 5 in this case.

Here ends todays lesson.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.