1、 有一个字符串开头或结尾含有n个空格 (” abcdefgdddd ”),欲去掉前后空格,返回一个新字符串。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int delspace(char *p, char *buf)
{
if (p == NULL || buf == NULL)
{
return -1;
}
int begin = 0;
int end = strlen(p) - 1;
int n = 0;
//从左边开始
//如果当前字符为空,而且没有结束
while (p[begin] == ' ' && p[begin] != 0)
{
begin++; //位置从右移动一位
}
//如果当前字符为空,而且没有结束
while (p[end] == ' ' && p[end] != 0)
{
end--; //往左移动
}
n = end - begin + 1; //非空元素个数
strncpy(buf, p+begin, n);
buf[n] = 0;
return 0;
}
int main(void)
{
char *p = " abcddsgadsgefg ";
int ret = 0;
char buf[100] = { 0 };
ret = delspace(p, buf);
if (ret != 0)
{
return ret;
}
printf("buf = %s\n", buf);
printf("\n");
system("pause");
return 0;
}
2、有一个字符串“1a2b3d4e”,要求写一个函数实现如下功能:
功能1:把偶数位字符挑选出来,组成一个字符串1。
功能2:把奇数位字符挑选出来,组成一个字符串2。
功能3:把字符串1和字符串2,通过函数参数,传送给main,并打印。
功能4:主函数能测试通过。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int getstr1str2(char *source,char *buf1,char *buf2)
{
if(source == NULL || buf1 ==NULL || buf2==NULL)
{
return -1;
}
int n = strlen(source);//字符串长度
int i = 0;
for(i = 0;i < n;i++)
{
//如果奇数位
if(i % 2 != 0)
{
*buf1=source[i] ;
buf1++;
}
else//如果偶数位
{
*buf2=source[i] ;
buf2++;
}
}
*buf1=0;
*buf2=0;
return 0;
}
int main(void)
{
char *p="1a2b3c4d";
char buf1[100]={0};
char buf2[100]={0};
int ret = 0;
ret=getstr1str2(p,buf1,buf2);
if(ret != 0)
{
printf("getstr1str2 Error:%d\n",ret);
return ret;
}
printf("buf1=%s\n",buf1);
printf("buf2=%s\n",buf2);
printf("\n");
system("pause");
return 0;
}
3.
键值对(“key = value”)字符串,在开发中经常使用
要求1:请自己定义一个接口,实现根据key获取.
要求2:编写测试用例。
要求3:键值对中间可能有n多空格,请去除空格
注意:键值对字符串格式可能如下:
"key1 = value1"
"key2 = value2"
"key3 = value3"
"key4 = value4"
"key5 = "
"key6 ="
int getKeyByValue(char *keyvaluebuf, char *keybuf, char *valuebuf, int * valuebuflen);
int main(void)
{
//...
getKeyByValude("key1 = valude1", "key1", buf, &len);
//...
return 0;
}