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(charkexaluebuf, 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;
}