Topic:
Key-value pairs ("key = Valude") strings, which are often used in development;
Requirement 1: Please define an interface for you to obtain valude;40 points according to key
Requirement 2: Write test cases. 30 min
Requirement 3: The key value pair may have more than n spaces in the middle, please remove the space 30 points
Note: The key-value pair string format might look like this:
"Key1 = Valude1"
"Key2 = Valude2
"Key3 = Valude3"
"Key4 = Valude4"
"Key5 ="
"Key6 ="
"Key7 ="
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <ctype.h>5 6 intTRIMSPACESTR (Char*p,Char*buf);7 intGetkeybyvalue (Char*pkeyvalue,Char*pkey,Char*pValue);8 9 intMain ()Ten { One intRET =0; A CharKeyvalude[] ="Key1 = Valude1"; - CharKey[] ="Key1"; - Charvalude[1024x768] = {0}; the -RET =Getkeybyvalue (Keyvalude, Key, valude); - - if(Ret! =0) + { -printf"func getkeybyvalue () err:%d \ n", ret); + returnret; A } atprintf"valude:%s \ n", valude); - -System"Pause"); - return 0; - - } in - //string two-block model, the main function allocates memory (in), remove the extra space before and after the string to intTRIMSPACESTR (Char*p,Char*buf) + { - intRV =0; the Char*str =p; * inti =0;//Search pointer variables, starting at the head of the string $ intj = strlen (str)-1;//search for pointer variables, starting at the end of the stringPanax Notoginseng intLen =0;//valid length of string - the if(Str==null | | buf==NULL) + { ARV =-1; theprintf"Function trimspacestr () Check (Str==null | | buf==null) Error:%d", RV); + returnRV; - } $ $ while(Isspace (Str[i]) && i<j) - { -i++; the } - Wuyi while(Isspace (Str[j]) && j>=0) the { -j--; Wu } - AboutLen = j-i+1; $ -strncpy (BUF, str+I, Len); - -Buf[len] =' /'; A returnRV; + } the - intGetkeybyvalue (Char*pkeyvalue,Char*pkey,Char*pValue) $ { the //1. Determine if there is a key value passed in the parameter the Char*temp =Pkeyvalue; the intRV =0; the -temp =strstr (Pkeyvalue, PKey); in if(temp==NULL) the { theRV =-1; Aboutprintf"func getkeybyvalue () err:%d find no keyword pkey \ n", RV); the returnRV; the } theTemp = temp + strlen (pKey);//The secondary pointer points to the Y-letter of key at this point, and prepares for the next search + - //2. Then look for the position of = thetemp = strstr (temp,"=");//Search starting at current temp location =Bayi if(temp = =NULL) the { theRV =-2; -printf"func getkeybyvalue () err:%d Lookup not = \ n", RV); - returnRV; the } theTemp = temp +1;//The secondary pointer points at this point = back one, preparing for the next search the the //3. Extract Value -RV =trimspacestr (temp, pValue); the if(RV! =0) the { theprintf"func trimSpaceStr03 () err:%d \ n", RV);94 returnRV; the } the returnRV; the}
Summarize:
1.XXXXXXXXXXXXXXXXXX
2.XXXXXXXXXXXXXXXXXX
3.XXXXXXXXXXXXXXXXXX
String--getvaluebykey () synthetic Exercises