Solved

Finding a Replacing a string in Visual C++

Posted on 2000-05-16
3
188 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

740 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