无题(三):键值对的操作(运用指针)

<pre name="code" class="cpp">////键值对(”key = valude”)字符串,在开发中经常使用;
////要求1:请自己定义一个接口,实现根据key获取valude;
////要求2:编写测试用例。
////要求3:键值对中间可能有n多空格,请去除空格
////注意:键值对字符串格式可能如下:
////“key1 = valude1”
////“key2 = valude2
////“key3 = valude3”
////“key4 =   valude4  ”
////“key5 = “
////“key6 = “
////“key7 = “
//方法一:
int removeSpace(char* inbuf, char* outbuf, int* count)
{
	int ret = 0;//定义返回值变量,并初始化为正常状态
	int charSize = 0;//记录字符串中字符个数
	int i, j;
	i = 0;     //定义输入字符串的起始端
	j = strlen(inbuf) - 1;///定义字符串的末端
	if (inbuf == NULL || outbuf == NULL)
	{
		ret = -1;//返回异常
		return ret;
	}
	else
	{
		while (inbuf[i] == ' ' && inbuf[i] != '\0')
		{
			i++;//记录字符串头部的空格个数
		}
		while (inbuf[j] == ' ' && inbuf[j] != '\0')
		{
			j--; //记录字符串尾部的空格个数
		}
		charSize = j - i + 1;//求出字符串中的非空格字符
		*count = charSize;
		strncpy(outbuf, (inbuf + i), charSize);
	}
	return ret;
}

int get_value(char* instr, char* key, char* value,int* count)
{
	int ret = 0;//定义返回值,并初始化为正常状态
	if (instr == NULL || key == NULL)
	{
		ret = -2; //错误状态
		printf("字符串instr或key为NULL");
		return ret;
	}
	char* p = NULL;//定义中间指针变量
	p = strstr(instr,key);//判断字符串中是否含有key指针指向的字符串
	if (p == NULL)
	{
		ret = -3;//错误状态
		printf("未找到key值");
		return ret;
	}
	p = strstr(instr, "=");//判断字符串中是否含有‘=’
	if (p == NULL)
	{
		ret = -4;
		printf("字符串中未找到‘=’");
		return ret;
	}
	p = p + 1;//指向等号后面的第一个字符
	ret = removeSpace(p, value, count);
	if (ret == -1)
	{
		printf("removeSpace()函数出错!!!");
		return ret;

	}
	return ret;
}
int test1()
{
	char my_instr[] = "key4 =    valude4     ";
	char my_key[] = "key4";
	char my_value[1024] = {0};
	int my_count = 0;//记录空格数目
	get_value(my_instr, my_key, my_value, &my_count);
	printf("%s\n", my_value);
	return 0;
}
//方法二:
int getKeyByValude(char *keyvaluebuf, char *keybuf, char *valuebuf, int * valuebuflen)
{
	int ret = 0;  //定义返回值 ,并初始化正常状态
	char *outbuf = keybuf;
	int len = 0;
	//参数检测
	if (keyvaluebuf == NULL || keybuf == NULL || valuebuf == NULL || valuebuflen == NULL)
	{
		ret = -1; //错误状态
		printf("keyvaluebuf==NULL|| keybuf==NULL||valuebuf==NULL||valuebuflen==NULL ERRO_MSG:%d\n", ret);
		return ret;
	}
	while (*keyvaluebuf++ == *outbuf++);//是指针跳过key值指向等号
	
	while (*keyvaluebuf)
	{
		if (*keyvaluebuf == '=')
		{
			len = 0;
			outbuf = valuebuf;//将valuebuf地址赋值给outbuf指针
			//keyvaluebuf++;

		}
		if (*keyvaluebuf == ' ')
		{
			//keyvaluebuf++;
		}
		else
		{
			*outbuf = *keyvaluebuf;  //将每一个value字符赋值给outbuf所指向的内存区域
			outbuf++;
			len++;
			//keyvaluebuf++;
		}
		
		keyvaluebuf++;
	}
	*outbuf = '\0';
	*valuebuflen = len;

	return ret;
}
int test2()
{
	char str1[] = "ORACLE_name = helloworld";
	char str2[] = "oracle_pwd = 123456";
	char str3[] = "oracle_pwd = 123456";
	char str_key[20]="ORACLE_name";
	char str_value[20];
	int  str_len = 0;
	int ret = getKeyByValude(str1, str_key, str_value, &str_len);
	if (ret)
	{
		printf("函数调用出错!\n");
	}
	else
	{
		printf("key=%s\nvalue=%s\nvaluelen=%d", str_key, str_value, str_len);
	}
	system("pause");
}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值