2 & mdash; the length of the string to be replaced and the length of the string to be replaced can be different.

Source: Internet
Author: User
Tags strfind

Implement string neutron string replacement 2 -- the length of the string to be replaced and the string to be replaced can be different.

 

// Use the C language to replace the string neutron string // Description: Write a string replacement function, for example, the function name is StrReplace (char * strSrc, char * strFind, char * strReplace ), // strSrc is the original string, strFind is the string to be replaced, and strReplace is the replacement string. // An intuitive example, for example, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", replace "RST" with "ggg", and the result is changed: ABCDEFGHIJKLMNOPQgggUVWXYZ # include <stdio. h> # include <string. h> void StrReplace (char * strSrc, char * strFind, char * strReplace) {int I, j, k, m; int lengthSrc, lengthFind, lengthReplace; lengthSrc = strlen (strSrc); lengthFind = strlen (strFind); lengthReplace = strlen (strReplace); for (I = 0; I <lengthSrc;) {j = 0; if (strSrc [I] = str Find [j]) // traverses the original string. If the current character is equal to strFind [0], start to judge {do {I ++; j ++;} by bit ;} while (j <lengthFind) & (strSrc [I] = strFind [j]); // judge the condition for jumping out of the while loop. If j = lengthFind, the matching string is found. And I points to the next character matching the strFind string in strSrc. If (j = lengthFind) {// if (lengthFind = lengthReplace) {for (k = I-lengthFind, m = 0; m <lengthReplace; k ++, m ++) {strSrc [k] = strReplace [m] ;}} // The strFind string length is smaller than the strReplace String Length. if (lengthFind <lengthReplace) {// increase the vacancy. For (k = lengthSrc; k> = I; k --) {strSrc [k + lengthReplace-lengthFind] = strSrc [k];} // update the strSrc length. If this parameter is not updated, the next time you add a blank space, there will be a problem. LengthSrc + = lengthReplace-lengthFind; // start replacement. For (k = I-lengthFind, m = 0; m <lengthReplace; k ++, m ++) {strSrc [k] = strReplace [m];} // After the element and length of strSrc are changed, you need to point I to the next character of the replaced part. I + = lengthFind-lengthReplace;} // The length of the strFind string is greater than the length of the strReplace string if (lengthFind> lengthReplace) {// reduce the vacancy. For (k = I; k <= lengthSrc; k ++) {strSrc [k-(lengthFind-lengthReplace)] = strSrc [k];} // update the strSrc length. If this parameter is not updated, a problem may occur when the vacancy is reduced in the next time. LengthSrc-= lengthFind-lengthReplace; // start replacement. For (k = I-lengthFind, m = 0; m <lengthReplace; k ++, m ++) {strSrc [k] = strReplace [m];} // After the element and length of strSrc are changed, you need to point I to the next character of the replaced part. I-= lengthFind-lengthReplace ;}} else {I ++ ;}} int main () {char strSrc [255], strFind [255], strReplace [255]; gets (strSrc); gets (strFind); gets (strReplace); StrReplace (strSrc, strFind, strReplace); puts (strSrc); return 0 ;}

Can word or notepad replace two different strings at the same time? That is, if you want to complete the replacement twice

Different strings must be associated with each other. Otherwise, they can only be replaced twice.
For example, replacing "My, his" with "her" can be completed once.
Press CTRL + H to open the replace dialog box, click Advanced, and select "use wildcard"
In the search content box, enter? Of
Enter her
Click replace all"

Replace the n characters starting from the m character in string s1 with string s2. The s2 length is not necessarily equal to n. Therefore, the length of the replaced character can be changed.

# Include <stdio. h>
Char * exchange (char * first, char * second, int index, int count) // The method used here is very good, no difficulty
// Input parameter 1 is the string to be inserted, 2 is the string to be inserted, and 3 is the start position, including index
// Bit. count indicates the number of characters to replace.
{
Char * temp = new char [100]; // The space must be dynamically allocated using new or malloc (as if written in this way,
// Otherwise, the function ends and the defined array is gone.
For (int I = 0; I <= index & first [I]! = '\ 0'; I ++) // three for loops are required to complete. This is a stupid method.
{
Temp [I] = first [I];
}
Int j = 0;
For (; second [j]! = '\ 0'; j ++)
{
Temp [index + j] = second [j];
}
For (int m = 0; first [m]! = '\ 0'; m ++)
{
Temp [index + j + m] = first [index + count + m];
}
Return temp; // the pointer to the returned array temp
}
Void main ()
{
Char * a = "afdadfgg"; // If a value is assigned in the form of {'','', ''}, the last character must be assigned to '\ 0'
Char * B = "lkl ";
A = exchange (a, B, 2, 2 );
Printf ("the inserted character is % s \ n", );
}
// In fact, after you learn C ++, you will find that it is very easy to implement such an operation, but do more such programming at ordinary times.
// Very good

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.