题目
函数实现
函数一
//去除字符串前后的空格
int trimSpace(char* inbuf, char* outbuf)
{
int ret = 0;
char* tempinbuf = inbuf; //临时指针指向原字符串首地址
char* tempoutbuf = outbuf;
//判断地址是否有效
if (inbuf == NULL || outbuf == NULL)
{
ret = -1;
printf("func trimSpace()err(inbuf == NULL || outbuf == NULL): %d", ret);
return ret;
}
//让临时指针跳过前面的空格
while (*tempinbuf == ' ')
{
//这里跳出循环时候,tempinbuf已经指向空格后面的字符的位置
tempinbuf++;
}
/*printf("输出tempinbuf字符串:%s\n", tempinbuf);*/
//当遇到空格时结束赋值
while (*tempinbuf != ' ')
{
*tempoutbuf++ = *tempinbuf++;
}
*tempoutbuf = '\0'; //在跳出循环后tempoutbuf指向的的位置在最后一个字符的后面地址,需要赋值为'\0'
/*printf("输出原始字符串:%s\n", inbuf);
printf("输出字符串:%s\n", outbuf);*/
return ret;
}
函数二
int getStr1Str2(char* source, char* buf1, char* buf2)
{
//用临时指针接收传入的地址
char* temps = source;
char* tempbuf1 = buf1;
char* tempbuf2 = buf2;
//判断是否为空,并返回
int ret = 0;
if (temps == NULL || tempbuf1 == NULL || tempbuf2 == NULL)
{
ret = -1;
printf("func getStr1Str2()err: temps == NULL || tempbuf1 == NULL || tempbuf2 == NULL: %d", ret);
return ret;
}
//原理
for (int i = 0; *temps != '\0'; i++)
{
if (i % 2 == 0)
{
*tempbuf1++ = *temps++; //这个意思是先执行*tempbuf1 = *temps; 》》》 再执行 tempbuf1++和temps++
}
else
{
*tempbuf2++ = *temps++;
}
}
*tempbuf1 = '\0'; //在跳出循环后tempbuf1指向的的位置在最后一个字符的后面地址,需要赋值为'\0'
*tempbuf2 = '\0';
//打印一下数据,可注释掉
/*printf("source: %s\n", source);
printf("buf1: %s\n", buf1);
printf("buf2: %s\n", buf2);*/
return ret;
}
函数三
int getKeyByValue(char* keyvaluebuf/*in*/, char* keybuf/*in*/, char* valuebuf/*out*/, int* valuebuflen)
{
int ret = 0;
//临时指针接一下
char* tempkeyvaluebuf = keyvaluebuf;
char* tempkeybuf = keyvaluebuf; //先都指向输入字符串的地址
char* tempvaluebuf = keyvaluebuf;
int* tempvaluebuflen = valuebuflen;
//1 为空返回-1错误
if (tempkeyvaluebuf == NULL || tempkeybuf == NULL || tempvaluebuf == NULL || tempvaluebuflen == NULL)
{
ret = -1;
printf("func getKeyByValue()err: tempkeyvaluebuf == NULL || tempkeybuf == NULL || tempvaluebuf == NULL || tempvaluebuflen == NULL: %d", ret);
return ret;
}
//2 判断是否有=,否则返回-2
int ifequal = strstr(tempkeyvaluebuf, "=");
if (!ifequal)
{
ret = -2;
printf("func getKeyByValue()err: there is no '=' : %d\n", ret);
return ret;
}
//原理
//以'='为标准把输入字符串分为两个
char* temptempfront = tempkeybuf;
while (*tempkeyvaluebuf != '=')
{
*temptempfront++ = *tempkeyvaluebuf++;
tempvaluebuf++;
}
tempvaluebuf = tempvaluebuf + 1;//指向字符串=后面的数据
*temptempfront = '\0'; //需要将最后一个值置为'\0'
//打印测试上述功能
/*printf("tempfront:\n%s\n", tempkeybuf);
printf("tempback:\n%s\n", tempvaluebuf);*/
//调用函数trimSpace去除空格
trimSpace(tempkeybuf, tempkeybuf);
trimSpace(tempvaluebuf, tempvaluebuf);
//打印测试上述功能
/*printf("after trimSpace\n");
printf("tempfront:\n%s\n", tempkeybuf);
printf("tempback:\n%s\n", tempvaluebuf);*/
//
//if (tempkeybuf == keybuf)
//{
// valuebuf = tempvaluebuf; //这是传的形参地址,错误!!!
// *valuebuflen = strlen(valuebuf);
//}
//通过指针间接给value赋值
//3 判断key是否在str中的=号左边
int cmp = strcmp(keybuf, tempkeybuf);
if (cmp == 0)
{
*valuebuflen = strlen(tempvaluebuf);
while (*tempvaluebuf != '\0')
{
*valuebuf++ = *tempvaluebuf++;
}
*valuebuf = '\0';
}
else
{
ret = -3;
printf("no key has been matched....err():%d\n", ret);
return ret;
}
return ret;
}
测试代码
int main()
{
/****************************************************************************/
/***************************测试trimSpace************************************/
printf("/***************************测试trimSpace*************************************/\n");
char* p = " sddasda ";
char out[20];
int ret = trimSpace( p, out);
if (ret != 0)
{
printf("func trimSpace()err(inbuf == NULL || outbuf == NULL): %d", ret);
return ret;
}
printf("原始字符串的长度:%d\n", strlen(p));
printf("转换后字符串的长度:%d\n", strlen(out));
printf("输出转换后的字符串:%s\n", out);
printf("/****************************************************************************/\n");
/****************************************************************************/
/****************************************************************************/
/***************************对getStr1Str2的测试******************************/
printf("/***************************对getStr1Str2的测试******************************/\n");
char p1[15];
char p2[15];
int ret2 = getStr1Str2(out, p1, p2);
if (ret2 != 0)
{
printf("func getStr1Str2 err: %d", ret2);
return ret2;
}
printf("source: %s\n", out);
printf("buf1: %s\n", p1);
printf("buf2: %s\n", p2);
printf("/****************************************************************************/\n");
/************************************************************************/
/************************************************************************/
/***************************对getKeyByValue的测试************************/
printf("/***************************对getKeyByValue的测试******************************/\n");
char keyandvalue[] = " xgq 9 asdihsio2 ";
char keybuf[100] = "xgq";
char valuebuf[100];
int valuelen;
int ret3 = getKeyByValue(keyandvalue, keybuf, valuebuf, &valuelen);
if (ret3 != 0)
{
printf("getKeyByValue err: %d", ret3);
return ret3;
}
printf("keybuf:%s\n", keybuf);
printf("valuebuf:%s\n", valuebuf);
printf("valuelen:%d\n", valuelen);
printf("/****************************************************************************/\n");
system("pause");
return 0;
}