C language Implementation name value to find value by key

Source: Internet
Author: User

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

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.