C语言字符串例题

C语言字符串例题


第一题:函数实现字符串两端去除空格字符

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

/**
  *有一个字符串开头或结尾含有n个空格 (”   abcdefgdddd    ”),
  *欲去掉前后空格,返回一个新字符串。
  *要求1:请自己定义一个接口(函数),并实现功能;
  *要求2:编写测试用例。
  *int trimSpace(char *inbuf, char *outbuf);
  */
/**
 * func:实现字符串的前后空格去除
 * param:
 *	inbuf:输入字符串
 *  outbuf:输出字符串
 * return:
 * 错误返回1.正常执行返回0
 */
int trimSpace(char *inbuf, char *outbuf)
{
	if (inbuf == NULL || outbuf == NULL)
	{
		return -1;
	}
	char *in = inbuf;
	char *out = outbuf;
	int length = strlen(in) - 1;
	char *end = in + length;
	while (isspace(*in))
	{
		in++;
	}
	while (isspace(*end))
	{
		end--;
	}

	length = end - in + 1;
	strncpy(out, in, length);
	return 0;
}
int main()
{
	char buf[] = "    abcdefgdddd    ";
	char bufOut[100] = { 0 };
	trimSpace(buf, bufOut);
	printf("bufOut = %s\n", bufOut);
	printf("\n");
	system("pause");
	return 0;
}

第二题:编程实现每隔一位取字符子串

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/**
 * 有一个字符串“1a2b3d4z”;
 * 要求写一个函数实现如下功能:
 * 功能1:把偶数位字符挑选出来,组成一个字符串1。
 * 功能2:把奇数位字符挑选出来,组成一个字符串2。
 * 功能3:把字符串1和字符串2,通过函数参数,传送给main,并打印。
 * 功能4:主函数能测试通过。
 * int getStr1Str2(char *source, char *buf1, char *buf2);
 */
int getStr1Str2(char *source, char *buf1, char *buf2)
{
	if (source == NULL || buf1 == NULL || buf2 == NULL)
	{
		return - 1;//代表异常
	}
	char *src = source;
	char *out1 = buf1;
	char *out2 = buf2;
	while (*src)
	{
		*out1++ = *src++;
		*out2++ = *src++;
	}
	//最后给字符串末尾添加0
	*out1 = 0;
	*out2 = 0;
	return 0;
}
int main()
{
	char buf[] = "1a2b3c4d";
	char buf1[20];
	char buf2[20];
	getStr1Str2(buf, buf1, buf2);
	printf("buf1 = %s\n", buf1);
	printf("buf2 = %s\n", buf2);

	
	printf("\n");
	system("pause");
	return 0;
}

第三题:键值对问题

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

//注: char* strstr(const char*src,const char* subsrc);返回字符串第一次出现的位置
/**
 * 键值对(“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 trimSpace(char *inbuf, char *outbuf);
int getKeyByValue(char *keyvaluebuf, char *keybuf, char *valuebuf, int * valuebuflen)
{
	if (keyvaluebuf == NULL || keybuf == NULL || valuebuf == NULL || valuebuflen == NULL)
	{
		return -1;
	}
	//查找pos第一次出现的位置
	char* pos = strstr(keyvaluebuf, keybuf);
	if (pos == 0)
	{
		return -2;//此时代表当前无此键
	}
	char* valuebufTmp = strstr(keyvaluebuf, "=")+1;
	trimSpace(valuebufTmp, valuebuf);
	*valuebuflen = strlen(valuebuf);
}
int trimSpace(char *inbuf, char *outbuf)
{
	if (inbuf == NULL || outbuf == NULL)
	{
		return -1;
	}
	char *in = inbuf;
	char *out = outbuf;
	int length = strlen(in) - 1;
	char *end = in + length;
	while (isspace(*in))
	{
		in++;
	}
	while (isspace(*end))
	{
		end--;
	}

	length = end - in + 1;
	strncpy(out, in, length);
	*(out + length) = 0;
	return 0;
}
int main()
{
	char valuebuf[20];
	int valuebuflen = 0;
	getKeyByValue("key1 =   valude1  ", "key1", valuebuf, &valuebuflen);
	printf("value = %s\n", valuebuf);
	printf("valuebuflen = %d\n", valuebuflen);
	printf("\n");
	system("pause");
	return 0;
}
发布了30 篇原创文章 · 获赞 58 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/annjeff/article/details/88081199