c语言中键值输入
来源:互联网 发布:电子琴与电钢琴 知乎 编辑:程序博客网 时间:2024/06/05 15:37
/*
键值对(”key = valude”)字符串,在开发中经常使用;
要求1:请自己定义一个接口,实现根据key获取valude;40分
要求2:编写测试用例。30分
要求3:键值对中间可能有n多空格,请去除空格30分
注意:键值对字符串格式可能如下:
“key1 = valude1”
“key2 = valude2
“key3 = valude3”
“key4 = valude4”
“key5 = “
“key6 =“
“key7 = “
*/
int trimSpace012(char *str, char *newstr) //除去空格得到字符串
{
char *p = str;
int ncount = 0;
int i, j = 0;
if (str==NULL||newstr==NULL)
{
printf("func str");
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;
}
//找到键值后面的字符串并打印
int getKeyByValude(char *keyvaluebuf, char *keybuf, char *valuebuf, int * valuebuflen) {
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= trimSpace012(p, valuebuf);
if (ret != 0)
{
printf("func getKeyByValude() err:%d\n", ret);
return ret;
}
return ret;
}
int main()
{
int ret = 0;
char buf[1024];
int buflen = 0;
char *keyandvalue = "key2 = valude2 ";
char *key = "key2";
ret = getKeyByValude(keyandvalue, key, buf, &buflen);
if (ret != 0)
{
printf("func getKeyByValude() err:%d\n", ret);
return ret;
}
printf("buf:%s\n", buf);
//getKeyByValude(“key1 = valude1”,” key1”, buf, &len);
system("pause");
return ret;
}
- c语言中键值输入
- c语言中查看输入输入地址
- 如何读取Linux键值,输入子系统,key,dev/input/event,dev/event,C语言键盘
- C语言中scanf格式化输入函数
- C语言中scanf格式化输入函数
- C语言中scanf格式化输入函数
- C语言中scanf格式化输入函数
- C语言中输入结束符EOF
- C语言中接受控制台的输入
- C语言中字符串的输入问题
- c语言中查看输入地址
- C语言中隐藏输入的代码
- C语言提取(key,value)键值对
- C语言之输入
- c 语言输入
- 再看C语言输入
- C语言输入终止
- c语言输入菱形
- 闪屏页的正确方式
- Android数据库无缝升级方案
- Hibernate Validation使用示例及讲解
- Android静默安装和静默卸载
- Python爬虫库学习笔记-BeautifulSoup
- c语言中键值输入
- Java整数与字符串转换
- Groovy使用多赋值
- RecyclerView特性和使用
- HDU 6016 Count the Sheep (思路)
- python-set
- Jquery EasyUI后台基本用法总结
- ios开发蓝牙 BLE
- 【web前端自动化工作环境配置】8. 依赖管理工具bower的基本用法