?
Solved

How do you do character string replacement?

Posted on 1998-10-28
4
Medium Priority
?
250 Views
Last Modified: 2010-04-15
Hello,

What is the easiest way to do string replacement?

For instance if I want to replace all occurances of the word "old" with the word "new" in the following string = "the old brown fox is very old"

In PERL is would be something like: $var =~ s/old/new/g;
0
Comment
Question by:edwardk091997
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 1253881
The best thing to use for that is the function strstr. It is not as powerful as perls solution obviously, but it will do.

char *strstr(char *string1, char *string2)

will find the first occurrence of string2 in string1. So a loop of

int i;
char *p;

while((p=strstr(string,rpl))!=NULL)
{   for(i=0; i<strlen(new); ++i)p[i]=new[i];
}

should do what you need.

0
 
LVL 16

Expert Comment

by:imladris
ID: 1253882
Note that strcpy isn't used for the actual replacement, since it will take along the trailing null byte. This would prematurely end the target string.


0
 
LVL 3

Expert Comment

by:arunm
ID: 1253883
This will only work if the string lengths of "old" and "new" are the same. In this case it is (3). but this is not a general solution. for a generic solution you must copy to another larger buffer.

try:-

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

void main()
{
      char string[] = "the old brown fox is very old";
      char output[256];
      int count1= 0,count2 = 0;

      while (count1 < strlen(string))
            if (!strncmp(&string[count1],"old", strlen("old")))
            {
                  strcpy(&output[count2], "new");
                  count1+=strlen("new");
                  count2+=strlen("new");
            }
            else
                  output[count2++] = string[count1++];

}

you could get rid on the repeated use of strlen to increase efficency.

0
 

Author Comment

by:edwardk091997
ID: 1253884
the comment from  arunm was a better solution....the strings can be different in size...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question