c语言习题(2)--键值对,字符串反转

来源:互联网 发布:java rmi 框架 编辑:程序博客网 时间:2024/06/01 10:30

1.思路一

/****3、键值对("key = valude")字符串,在开发中经常使用;要求1:请自己定义一个接口,实现根据key获取valude;要求2:编写测试用例。要求3:键值对中间可能有n多空格,请去除空格注意:键值对字符串格式可能如下:"key1 = valude1""key2 =       valude2""key3  = valude3""key4        = valude4""key5   =   ""key6   =""key7   =   "***//***作者:一叶扁舟作用:键值对时间:15:32 2017/5/7***/#include <stdio.h>#include <stdlib.h>#include <string.h>//去空格int trimSpace1(char *inbuf, char *outbuf){char * result = outbuf;if (inbuf == NULL || outbuf == NULL){return -1;}while (*inbuf != '\0'){if (*inbuf == ' '){inbuf++;}else{*result = *inbuf;result++;inbuf++;}}*result = '\0';return 1;}int getKeyByValude(char *keyvaluebuf,char *keybuf, char *valuebuf, int * valuebuflen){if (keyvaluebuf == NULL || keybuf == NULL || valuebuf == NULL){return -1;}char tempString[200];char *p = tempString;char *key = keybuf;char *value = valuebuf;char buff[100];int i = 0;int length = 0;//将传入的字符串过滤掉空格trimSpace1(keyvaluebuf, tempString);//首先将等号左边的key取出来,即while ( p[i]!= '='){i++;}memcpy(buff,p,i);buff[i] = '\0';//找到对应的key值if (strcmp(buff, key) == 0){int j = strlen(p) - i -1;memcpy(valuebuf,p +(i+1),j);valuebuf[j] = '\0';*valuebuflen = j;}else{//没有*valuebuflen = 0;valuebuf[0] = '\0';}return 1;}void main(){char *keyValuSource = "name = valude1";char key[100]="name1" ;char value[100];int valueLength = 0;int temp = getKeyByValude(keyValuSource, key, value, &valueLength);if (temp == 1){printf("key:%s\n",key);printf("value:%s\n", value);printf("value的长度:%d\n", valueLength);}else{printf("调用出错\n");}printf("\n");system("pause");}


2.思路二

#include "stdio.h"#include "stdlib.h"#include "string.h"//一般情况下不要修改输入的内存块的值int trimSpace_ok2(char *mybuf, char *outbuf){int count = 0;int i = 0, j = 0;char *p = mybuf;j = strlen(p) - 1;while (isspace(p[i]) && p[i] != '\0'){i++;}while (isspace(p[j]) && j>0){j--;}count = j - i + 1;//printf("count:%d", count);//void *  __cdecl memcpy(void *, const void *, size_t);memcpy(outbuf, mybuf + i, count);outbuf[count] = '\0';return 0;//system("pause");}int getKeyByValude2(char *keyvaluebuf /*in*/, char *keybuf  /*in*/,char *valuebuf /*in out*/, int * valuebuflen /*in out*/){int rv = 0;char tmp[2048 * 10];char *p = NULL;//1 在大字符串里面查找有么有关键字         //strstr()函数搜索一个字符串在另一个字符串中的第一次出现。//该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。p = strstr(keyvaluebuf, keybuf);if (p == NULL){return 0;}p = p + strlen(keybuf);//2 在查找=号p = strstr(keyvaluebuf, "=");if (p == NULL){return 0;}p = p + 1;//3 去掉左右空格rv = trimSpace_ok2(p, tmp);if (rv != 0){printf("func trimSpace_ok2() err:%d\n", rv);return rv;}strcpy(valuebuf, tmp);*valuebuflen = strlen(tmp);return 0;}void main(){int rv = 0;char keyvaluebuf[] = "ORACLE_name  =  itcast     ";char *keybuf = "ORACLE_name";char valuebuf[1024];int valuebuflen = 0;//调用函数,要先判断是否出错rv = getKeyByValude(keyvaluebuf, keybuf, valuebuf, &valuebuflen);if (rv != 0){printf("func getKeyByValude() err:%d", rv);return;}printf("valuebuf:%s\n", valuebuf);printf("valuebuflen:%d\n", valuebuflen);system("pause");}

3.字符串反转

/**字符串反转,取出数据"abcdefghjklmnopqrst"**//***作者:一叶扁舟作用:字符串反转时间:18:24 2017/5/6***/#include <stdio.h>#include <stdlib.h>#include <string.h>int trimSpace(char *inbuf, char *outbuf){char * result = outbuf;if (inbuf == NULL || outbuf == NULL){return -1;}while (*inbuf != '\0'){if (*inbuf == ' '){inbuf++;}else{*result = *inbuf;result++;inbuf++;}}*result = '\0';return 1;}int switchString(char *inBuf){if (inBuf == NULL){return -1;}int i = strlen(inBuf);char *p1 = NULL, *p2 = NULL;char temp;p1 = inBuf;//指向首字符p2 = inBuf + i - 1;//指向尾字符while (p1 != p2){temp = *p1;*p1 = *p2;*p2 = temp;p1++;p2--;}return 0;}void main(){char buf1[100] = "abcdefghjklmnopqrst";char buf2[100];int temp = switchString(buf1);if (temp != -1){printf("%s\n", buf1);}system("pause");}



0 0
原创粉丝点击