Solved

Finding a Replacing a string in Visual C++

Posted on 2000-05-16
3
184 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

728 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

10 Experts available now in Live!

Get 1:1 Help Now