Solved

How do you do character string replacement?

Posted on 1998-10-28
4
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

705 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