c语言 字符串模型的作业题

涉及到的函数

  1. //C 库函数 char *strncpy(char *dest, const char *src, size_t n)
    //把 src 所指向的字符串复制到 dest,最多复制 n 个字符。
    //当 src 的长度小于 n 时,dest 的剩余部分将用空字节填充

2.//C 库函数 int isspace(int c) 检查所传的字符是否是空白字符。
//如果 c 是一个空白字符,则该函数返回非零值(true),否则返回 0(false)。

3.//C 库函数 char *strstr(const char *haystack, const char *needle)
//在字符串 haystack 中查找第一次出现字符串 needle 的位置,不包含终止符 ‘\0’。
//该函数返回在 haystack 中第一次出现 needle 字符串的位置,如果未找到则返回 null。

3.//c语言中没有字符串类型,通过字符数组来模拟字符串
//C 库函数 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符
//sizeof(类型)字符串类型,的大小,包括\0;
//C语言中的字符串是以’\0’结束的字符数组

4.//C 库函数 char *strcpy(char *dest, const char *src) 把 src 所指向的字符串复制到 dest

1.function:去除字符串两端的空格

//function:去除字符串两端的空格
//核心思路:设置两个指针,一个指向头,一个指向尾,然后通过++ 和-- 向中间同时逼近
//利用isspace判断是否是空,当前后指向都不是空的时候跳出循环。
//将此时尾指针的后一位赋值为'\0'作为字符串结束标志
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
int trimSpace(char** inbuf, char** outbuf)
{
	int ret = 0;
	if ((*inbuf == NULL) || (*outbuf == NULL))
	{
		ret = -1;
		printf("(inbuf == NULL) || (outbuf == NULL),%d\n", ret);
		return ret;
	}
	char* inbuf_tmp = *inbuf; 
	//char* outbuf_tmp = *outbuf;
	char* end_tmp = inbuf_tmp + strlen(inbuf_tmp)-1;


	//C 库函数 int isspace(int c) 检查所传的字符是否是空白字符。
	//如果 c 是一个空白字符,则该函数返回非零值(true),否则返回 0(false)。
	while ((isspace(*inbuf_tmp))||(isspace(*end_tmp)))
	{
		if(isspace(*inbuf_tmp))
		{
			inbuf_tmp=inbuf_tmp+1; 
		}

		if (isspace(*end_tmp))
		{
			end_tmp = end_tmp - 1;
		}
	}
	*(end_tmp + 1) = '\0';
	*outbuf = inbuf_tmp;
	//*outbuf = end_tmp;
	return ret;
}
void main()
{
	int ret;
	char text[] = "   abcdef gdddd    ";
	char* inbuf = text;
	char* outbuf = NULL;
	outbuf = (char*)malloc(sizeof(char) * strlen(text));
	ret=trimSpace(&inbuf, &outbuf);



	if (ret != 0)
	{
		printf(" error \n");
	}

	printf("inbuf =%s", outbuf);
	return;
}

2.//fun:有一个字符串”1a2b3d4z”,;
//要求写一个函数实现如下功能,
//功能1:把偶数位字符挑选出来,组成一个字符串1。valude;20分
//功能2:把奇数位字符挑选出来,组成一个字符串2,valude 20
//功能3:把字符串1和字符串2,通过函数参数,传送给main,并打印。
//功能4:主函数能测试通过。

//fun:有一个字符串”1a2b3d4z”,;
//要求写一个函数实现如下功能,
//功能1:把偶数位字符挑选出来,组成一个字符串1。valude;20分
//功能2:把奇数位字符挑选出来,组成一个字符串2,valude 20
//功能3:把字符串1和字符串2,通过函数参数,传送给main,并打印。
//功能4:主函数能测试通过。

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

int getStr1Str2(char* source, char* buf1, char* buf2)
{
	int ret=0;
	int num = 0;
	char* tmp_source = source;
	
	int i=0;
	int j=0;
	while((*tmp_source) !='\0')
	{
		if ((num+2) % 2 == 0)//偶
		{
			*(buf1 + i) = *tmp_source;
			i++;
		}
		else//奇
		{
			*(buf2 + j) = *tmp_source;
			j++;
		}
		tmp_source++; 
		num++; 
	}
	*(buf1 + i) = '\0';
	*(buf2 +j) = '\0';

	return ret;
}
void main12343()
{
	char text[] = "1a2b3d4z";
	char* source = text;
	char* buf1 = NULL;
	char* buf2 = NULL;
	buf1 = (char*)malloc(sizeof(char) * strlen(text));
	buf2 = (char*)malloc(sizeof(char) * strlen(text));

	int ret = getStr1Str2(source,buf1,buf2);
	printf("source= %s\n", source);
	printf("buf1=%s \n", buf1);
	printf("buf2=%s \n", buf2);


	return;
}

//键值对(”key = valude”)字符串
//要求1:请自己定义一个接口,实现根据key获取valude;
//要求2:编写测试用例。
//要求3:键值对中间可能有n多空格,请去除空格
//注意:键值对字符串格式可能如下:
//“key1 = valude1”
//“key2 = valude2
//“key3 = valude3”
//“key4 = valude4”
//“key5 = “
//“key6 = “
//“key7 = “

//键值对(”key = valude”)字符串
//要求1:请自己定义一个接口,实现根据key获取valude;
//要求2:编写测试用例。
//要求3:键值对中间可能有n多空格,请去除空格
//注意:键值对字符串格式可能如下:
//“key1 = valude1”
//“key2 = valude2
//“key3 = valude3”
//“key4 = valude4”
//“key5 = “
//“key6 = “
//“key7 = “


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

int trimSpace1(char* str, char* newstr)
{
    char* p = str;
    int ncount = 0;
    int i, j = 0;

    if (str == NULL || newstr == NULL)
    {
        printf("func trimSpace() \n");
        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;
    //C 库函数 char *strncpy(char *dest, const char *src, size_t n)
    //把 src 所指向的字符串复制到 dest,最多复制 n 个字符。
    //当 src 的长度小于 n 时,dest 的剩余部分将用空字节填充
    strncpy(newstr, str + i, ncount);

    newstr[ncount] = '\0';

    return 0;

}
int getKeyByValude(char* key_value_buf, char* key_buf, char* value_buf)
{
	int ret=0;
	char* tmp_buf= key_value_buf;
	//trimSpace1(key_value_buf, tmp_buf);
    tmp_buf = strstr(tmp_buf, key_buf);
    if (tmp_buf == NULL)
    {
        ret = -1;
        printf("the key is not found");
        return ret;
    }
    tmp_buf = strstr(tmp_buf, "=");
    
    if (tmp_buf == NULL)
    {
        ret = -1;
        printf("the = is not found");
        return ret;
    }
    tmp_buf++;
    trimSpace1(tmp_buf, value_buf);
    
    *(value_buf + strlen(value_buf)) = '\0';


	return ret;
}
void main8883()
{
	int ret;
	//int value_buf_len=7;
	char text1[] = "  key1  =  valude1  ";
	char text2[] = "key1";
	char* key_value_buf = text1;
	char* key_buf = text2;
	char* value_buf = NULL;
	value_buf = (char*)malloc(sizeof(char) * strlen(text1));
	
	ret=getKeyByValude(key_value_buf,key_buf,value_buf);
    if (ret != 0)
    {
        printf("error");

    }
    printf("the value is %s", value_buf);



}


//fun:使用递归完成字符串反转

//fun:使用递归完成字符串反转
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
//C 库函数 char *strncat(char *dest, const char *src, size_t n)
//把 src 所指向的字符串追加到 dest 所指向的字符串的结尾,直到 n 字符长度为止。

//C库函数 void* memset(void* str, int c, size_t n)
//复制字符 c(一个无符号字符)到参数 str 所指向的字符串的前 n 个字符.
void inverse(char *inbuf,char  *outbuf)
{
	if (inbuf == NULL||outbuf==NULL)
	{
		printf("inbuf is NULL");
		return;
	}
	if (*inbuf == '\0')//递归结束的条件
	{
		return;
	}
	inverse(inbuf+1, outbuf);//inbuf开始是指向第一个字符的地址,然后递归调用,将每个字符的地址都入栈,满足结束条件后,从栈顶部出栈
	strncat(outbuf, inbuf, 1);
	return;
}

int main()
{
	char buf[] = "abcd";

	char outbuf[1024] = { 0 };
	//memset(outbuf, '0', sizeof(outbuf));

	inverse(buf, outbuf);

	printf("outbuf = %s", outbuf);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值