requirements, such as:
1, "Key1 = value1" through "Key1" from the string to find out "value", value removal before and after the space
2, "Key1 = = value1", "Key1 = value1" double equals sign illegal
Header file:
#include <stdlib.h>#include<stdio.h>#include<string.h>
Function Prototypes:
void trim (char *strin /* in */, char *strout /* out */ void getValue (char * keyandvalue /* in */, " Span style= "COLOR: #0000ff" >char * key /* in */, char * value /* out */);
Implementation method:
1 voidTrimChar*strin,Char*strout) {2 3 Char*start, *end, *temp;//defines the tail and pointer of a string after a space is stripped4 5temp =Strin;6 7 while(*temp = =' '){8++temp;9 }Ten Onestart = temp;//obtaining the head pointer A -temp = Strin + strlen (strin)-1;//get a pointer to the last character of the original string (not ' + ') - the while(*temp = =' '){ ---temp; - } - +end = temp;//Obtain the tail pointer - + A for(Strin = start; Strin <=end;) { at*strout++ = *strin++; - } - -*strout =' /'; -}
1 voidGetValue (Char* Keyandvalue,Char* Key,Char*value) {2 3 Char*p =Keyandvalue;4 5p =strstr (Keyandvalue, key);6 if(p = =NULL) {7printf"no key\n .");8 return ;9 }Ten Onep + =strlen (key); A trim (p, value); - -p = strstr (value,"="); the if(p = =NULL) { -printf"no =\n ."); - return; - } +p+= strlen ("="); - trim (p, value); + Ap = strstr (value,"="); at if(P! =NULL) { -printf"extra =\n."); - return; - } -p =value; - trim (p, value); in -}
Test:
1 voidMain () {2 3 Char*keyandvalue ="Key1 = Kkkki";4 5 Char*key ="Key1";6 7 Charvalue[ -] = {0};8 9 GetValue (Keyandvalue, key, value);Ten Oneprintf"value = *%s*\n", value); ASystem"Pause"); -}
C language Implementation name value to find value by key