字符串函数strstr/strtok/strerror

本文介绍了C语言中的三个重要字符串处理函数:strstr用于查找子字符串,strtok用于分割字符串为标记,以及strerror用于获取错误消息。这些函数在程序开发中常用于字符串操作和错误管理。

1.1 strstr - Locate substring (在字符串里定位子字符串)

const char * strstr ( const char * str1, const char * str2 );
      char * strstr (       char * str1, const char * str2 );

str1

C string to be scanned.

str2

C string containing the sequence of characters to match.

即寻找str2在str1里第一次出现的位置;

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

The matching process does not include the terminating null-characters, but it stops there.

找到了就返回第一次出现位置的地址,找不到就返回空指针(NULL);

A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.

strstr 的模拟实现

const char* my_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	char* cp = (char*)str1;//保持str1和str2不变;
	char* s1; char* s2;

	if (str2 == NULL)//if (!*str2)//如果arr2为空指针,就直接返回str1;
		return (char*)str1;

	while (*cp)
	{

		s1 = cp;
		s2 = (char*)str2;

		while (*s1 && *s2 && *s1 == *s2)
		{
			s1++;
			s2++;

		}
		if (*s2 == '\0')
		{
			return cp;
		}
		cp++;
	}
	return (NULL);
	
}

int main()
{
	char arr1[] = "to be or not to be";
	char arr2[] = "be";
	printf("%s", my_strstr(arr1, arr2));
	return 0;
}

1.2 strtok - Split string into tokens 

char * strtok ( char * str, const char * delimiters );

> str 是一个指定的字符串,包含0个或多个第二个参数里面的分隔符标记;

> delimiters (参数是个字符串,定义了分隔符的字符集合;)C string containing the delimiter characters. These can be different from one call to another.

strtok函数找到str中的下一个标记,并将其用\0结尾,返回一个指向这个标记的指针。找到一个标记的时候,strtok函数将保存它在字符串中的位置,然后开始下一个标记的查找;如果字符串中不存在更多的标记,则返回NULL指针;

int main()
{
	char* p = "today @is a *fairy# tale";
	const char* del = "@*#";
	char arr[30];
	char* str = NULL;
	strcpy(arr, p);//将源数据拷贝一份;
	for (str = strtok(arr, del); str != NULL; str = strtok(NULL, del))//开始传非空指针,因为位置会被保留,所以后面传空指针就行;
	{
		printf("%s\n", str);
	}
	return 0;
}

注意:strtok函数会改变被操作的字符串,所以在使用strtok的时候要临时拷贝一份源字符串,并用拷贝的字符串进行操作;

1.3 strerror - Get pointer to error message string

char * strerror ( int errnum );

Interprets the value of errnum, generating a string with a message that describes the error condition as if set to errno by a function of the library. 
库函数在执行的时候,会发生错误,会将一个错误码存放在errno这个变量中;
The returned pointer points to a statically allocated string, which shall not be modified by the program. Further calls to this function may overwrite its content (particular library implementations are not required to avoid data races).

errno是C语言提供的一个全局变量,后面一旦产生新的错误就会覆盖之前的错误码,所以要即使检查;

The error strings produced by strerror may be specific to each system and library implementation.

/* strerror example : error list */
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main ()
{
  FILE * pFile;
  pFile = fopen ("unexist.ent","r");
  if (pFile == NULL)
    printf ("Error opening file unexist.ent: %s\n",strerror(errno));
  return 0;
}

考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值