La la-copy strings Based on keywords, and copy strings with keywords

Source: Internet
Author: User
Tags key string

La la-copy strings Based on keywords, and copy strings with keywords

Lab job-copy strings Based on keywords

Problem description: copy the source string to the target string:
If a keyword is specified, it ends with this keyword (excluding the keyword itself)
If the copy fails, an empty string is returned.
 

Specific requirements: implement the following function prototype: SafeStrcpy2KeyWord (), and call this function in the code to implement the above functions. The implementation of this function should consider various possible parameter values to ensure that the program does not crash. Int SafeStrcpy2KeyWord (char * pDestBuffer, // copy Destination Address
Char * pSourceString, // Source Address of the copy
Int nDestBufferSize, // length of the destination buffer to be copied
Char * szKeyWord); // specifies the key string
Return Value: the length of the copied string. If the copy fails, 0 is returned.


Solution requirements:
Input parameters
The input contains multiple groups of data and ends with END.
The first line of data in each group contains a source string with no space and the length is less than 256. The next line or multiple lines are keywords (the length is less than 16) Until the END ends. "NULL" indicates that the keyword string is NULL. At this time, the length of the output copy should be 0, and the copied string is empty (also expressed by "NULL", see below ).

Output Parameters
For each data set, the output copy length and the copied destination string are separated by spaces. If the target string is NULL, it is expressed as "NULL.

Example
Sample input:
/Home/tony/work_server/1/rtest/relayer. out
/Use/as the keyword
/T
/1/r
.
NULL
END // copy with the keyword "/", "/t", "/1/r", "." and "NULL" respectively
Sample output:
0 NULL
5/home
22/home/tony/work_server
38/home/tony/work_server/1/rtest/relayer
0 NULL

1 # include <iostream> 2 # include <cstring> 3 # include <string> 4 using namespace std; 5 6 int SafeStrcpy2KeyWord (char * pDestBuffer, // copy the destination address 7 char * pSourceString, // copy the source address 8 int nDestBufferSize, // copy the destination buffer length 9 char * szKeyWord ); // specify the key string 10 11 int main () {12 string s1, s2, s3 = ""; 13 cin> s1; // source string 14 while (cin> s2) {15 if (s2 = "END") {16 system ("PAUSE"); 17 return 0; 18 19} 20 21 if (s2 = "NULL") {22 cout <"0 NULL" <endl; 23 continue; 24} 25 int m = SafeStrcpy2KeyWord (& s3 [0], // The copied Destination Address 26 & s1 [0], // The copied source address 27 255, // The copied destination buffer length 28 & s2 [0]); // specify the key string 29} 30 31 return 0; 32 33} 34 int SafeStrcpy2KeyWord (char * pDestBuffer, // copy the destination address 35 char * pSourceString, // copy the source address 36 int nDestBufferSize, // copy the destination buffer length 37 char * szKeyWord) // specify the key string 38 {39 int len1 = strlen (pSourceString ), len2 = strlen (szKeyWord); 4 0 // source length: len141 // key String Length: len242 int I = 0, j = 0; 43 while (I <len1 & j <len2) {44 if (pSourceString [I] = szKeyWord [j]) {45 ++ I; ++ j; 46} 47 else {48 j = 0; ++ I; // j reset, I move one digit after 49} 50 51} 52 if (j> 0) {53 pDestBuffer = (char *) malloc (sizeof (char) * nDestBufferSize ); // apply for space 54 // The i-len2 is the starting position for successful match and also the length of the string to be copied 55 for (int e = 0; e <I-len2; e ++) {// assign values to 56 pDestBuffer [e] = pSourceString [e]; 57} 58 if (I-l En2! = 0) {59 cout <I-len2 <""; 60 for (int k = 0; k <I-len2; k ++) 61 cout <pDestBuffer [k]; 62 63 cout <"\ n"; 64 free (pDestBuffer); 65 return I-len2; 66} 67 68 if (I-len2 = 0) {69 cout <"0 NULL" <endl; 70} 71 return I-len2; 72} 73 if (j = 0) {74 cout <"0 NULL" <endl; 75 return I-len2; 76} 77}View Code
The basic implementation process is
1. Character pointer, such as char * p = "abcs", p actually points to the address of the first character of this string constant/character array.
2. strlen (s) function, evaluate the string length, such as strlen (p) = 4;
3. pointer to a string variable, http://www.cplusplus.com/reference/cstdlib/malloc/
Http://cpp.sh/
For example, string ss = ""; // empty string
Char * k = & ss [0];
K = (char *) malloc (sizeof (char) * size); // The applied space. The size is the number of characters to be assigned, which is larger.
For (int I = 0; I <100; I ++)
K [I] = 'a ';
Cout <k <endl;
Free (k); // release space

4. Simple pattern matching.

Finally, I would like to thank my roommates.

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.