Solved

How do you do character string replacement?

Posted on 1998-10-28
4
190 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 50 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now