?
Solved

Finding a Replacing a string in Visual C++

Posted on 2000-05-16
3
Medium Priority
?
190 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
[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
  • 3
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
Suggested Courses

770 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