C applet - key-value pair string

4. Key-value pair string

  • Requirement 1: Ask yourself to define an interface to obtain the value according to the key
  • Requirement 2: Write test cases
  • Requirement 3: There may be more than n spaces in the middle of the key-value pair, remove the spaces

Such as:

"key1= value1";
"key2=      value2    ";
"key3=value3    ";
"key4=      value4";...
  • 1
  • 2
  • 3
  • 4
  • 5

code

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/*=====================================
    键值对字符串在开发中经常使用
    要求1:求自己定义一个接口,实现根据key获取value
    要求2:编写测试用例
    要求3: 键值对中间可能有n多个空格,求去除空格
===========================================*/

//去掉字符串前后空格
int trimSpace1(char *str, char *newstr)
{
    char *p = str;
    int ncount = 0;
    int i, j = 0;

    if (str == NULL || newstr == NULL)
    {
        printf("func trimSpace() \n");
        return -1;
    }

    i = 0;
    j = strlen(p) - 1;


    while (isspace(p[i]) && p[i] != '\0')
    {
        i++;
    }

    while (isspace(p[j]) && p[j] != '\0')
    {
        j--;
    }
    ncount = j - i + 1;

    strncpy(newstr, str + i, ncount);

    newstr[ncount] = '\0';

    return 0;
}

//根据key获取value
int getValueByKey(char *keyvaluebuf,char *keybuf,char *valuebuf)
{
    char *p = NULL;
    int ret = 0;
    if (keyvaluebuf == NULL || keybuf == NULL || valuebuf == NULL)
    {
        return -1;
    }

    //1.查找key是不是在母串中
    p = keyvaluebuf;
    p = strstr(p, keybuf);
    if (p == NULL)
    {
        return -1;
    }
    //让辅助指针变量 重新达到下一次检索的条件
    p = p + strlen(keybuf);

    //2.看有没有=号
    p = strstr(p, "=");
    if (p == NULL)
    {
        return -1;
    }
    //让辅助指针变量 重新达到下一次检索的条件
    p = p + strlen("=");

    //3.在等号后面 去除空格
    ret = trimSpace1(p, valuebuf);
    if (ret != 0)
    {
        printf("func trimSpace1() err:%d \n",ret);
        return ret;
    }

    return 0;
}
int main()
{
    int ret = 0;
    int buf[1024];
    char *keyandvalue = "key2=      value2    ";
    char *key = "key2";
    ret = getValueByKey(keyandvalue,key,buf);
    if (ret != 0)
    {
        printf("func getKeyByValue() err:%d \n",ret);
        return ret;
    }
    printf("buf:%s\n",buf);
    system("pause");

    return ret;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103

operation result

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325910457&siteId=291194637