Solved

How do you do character string replacement?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

920 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

16 Experts available now in Live!

Get 1:1 Help Now