Solved

How do you do character string replacement?

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

786 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