Solved

Finding a Replacing a string in Visual C++

Posted on 2000-05-16
3
185 Views
Last Modified: 2010-04-02
I have a string equal to:
http://localhost/....

I want to replace the first 16 characters with http://ipAddress

I have the second string set up to replace the 1st 16 characters of the first string.  

I was trying to use strncpy(1st str, 2nd str, 16) but the 2nd string was greater than 16 characters and it overlapped overlapped the 1st string.  Can anyone give me a solution to this frustrating problem.  Thanks.
0
Comment
Question by:paulca
  • 3
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2815006
If the replacement text is the same length as the text to be replaced, you can use memcpy() to do this.  if they are different lengths, you will have to create the new string in a new buffer (probably using strcpy() and strcat()--although there woudl be other ways) and then copy this new string back over the original string.

examples follow.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2815014
To replace a section with a sectiopn of the same length:

char S[] = "123DEF";
char R[] = "ABC";

memcpy(S,R,3);

S now is "ABCDEF".  Note you can't uses strcpy() or strncpy() etc for this because they would place a NUL terminator character in the string.  i.e. if you did

strcpy(S,R);

S woudl be "ABC/0EF"  where the /0 is the a NUL character that marks the end of the string. i.e the string woudl be only "ABC".

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2815033
To replace with a different size section (either longer or shorter)

char S[10] = "12EFG";
char R[] = "ABCD";
char Buf[10];

strcpy(Buf,R); // Get start of string.
strcat(Buf,S+2); // Get new end of string.
strcpy(S,Buf); // Copy newly formed string over the original string.

S is now "ABCDEFG" Note that in this case, I had to specify a size for the original stirng (S) because that string was goign to get longer, so I had to make sure that S would have enough room for the longer string.  You have to be very careful about this when performing any operation that lengthens a string.

It is also possible to do this without using a 2nd buffer.  you can use memmove() to move the 2nd part of the string forward or backward in the array to its desired position, then use memcpy() to copy in the initial larger/smaller string.  but I thing the 1st method might be a little easier for you.

Using these NUL-terminated C-style strings is somewhat discouraged.  They are difficult to use and it is easy to make mistakes that corrupt memory, and these mistakes may be very hard to find.  I woudl recommend that you switch to using a C++ string class.  It is much easier to use and much much safer.

Let me know if you have any questions.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 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