定义一个接口,实现根据key获取valude练习

1.去除字符串左右空格

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>


int quspace(char *p,char *q)
{
  char *from = p;
  char *to = q;

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

  int ncount = j-i+1;
  strncpy(to,from+i,ncount);
  to[ncount] = '\0';
  return 0;
}



int main()
{
  char from[] = "   abcde   ";
  char to [20];
  quspace(from,to);
  printf("%s\n",to);
  return 0;
}

2./键值对""key = valude”)字符串,在开发中经常使用;
要求1:请自己定义一个接口,实现根据key获取yalude;40分
要求2:编写测试用例。30分s
要求3:键值对中间可能有n多空格,请去除空格30分
注意:键值对字符串格式可能如下
“key1 = valued1”
"key2 = valude2 "
“key3 = value3”
"key4 =valuede4 "
"key5 = "
"key6 = "
"key7 = "
intgetKeyBxMalude(char
kexaluebuf, charkeybuf, charvaluebuf,int
)
/

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>



int quspace(char *from,char *to)
{
    int ret = 0;
    if(from == NULL||to == NULL)
    {
        ret = -1;
        printf("quspace():  err\n");
        return ret;
    }

    char *p = from;
    int i = 0;
    int j = strlen(p)-1;
    while(isspace(p[i])&&p[i]!='\0')
    {
        i++;
    }
    while(isspace(p[j])&&p[j]!='\0')
    {
        j--;
    }

    int ncount = j-i+1;
    strncpy(to,p+i,ncount);
    to[ncount] = '\0';
    return ret;

}


int getKey(char *keyvaaluebuf,char *keybuf,char *valuebuf,int *len)
{
    int ret = 0;
    if(keyvaaluebuf ==NULL||keybuf == NULL||valuebuf==NULL||len == NULL)
    {
        ret = -1;
        printf("getKey() err!\n");
        return ret;
    }

    char *p = NULL;
    p = keyvaaluebuf;
    p = strstr(p,keybuf);
    if(p ==NULL )
    {
        return -1;
    }

    p = p+strlen(keybuf);

    p = strstr(p,"=");
    if( p==NULL )
    {
        return -1;
    }
    p = p+strlen("=");
    quspace(p,valuebuf);
    int l = strlen(valuebuf);
    *len = l;


    return ret;
}

int main()
{
   int ret = 0;
    char keyvalue[] = "     key2 =          valude2  ";
    char key[10] = "key2";
    char value [20] = {0};
    int length = 0;
    ret = getKey(keyvalue,key,value,&length);

    printf("%s=%s\n",key,value);
    printf("%d\n",length);
    if(ret != 0)
    {
        printf("getKey() err:%d\n",ret);
    }

    return ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值