⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strs.c

📁 * Function: * 1. Replace the first oldstr with newstr in srcstr * Arguments: * IN : * srcst
💻 C
字号:
/***********************************************************
**   @(#)  Strs.h
**      Purpose    : String Tools
**      Version    : 1.2.0
**      Date       : 1999/08/05
**      Developer  : Rick Zhang (Zhang MingDe)
***********************************************************/

#include <Strs.h>

/**********************************************************************************
*	Function:
*		1. Remove all spaces (' ') and Tab ('\t') in the end and in the
*			beginning of 'Str'
*	Arguments:
*		IN	:
*			Str
*		OUT	:
*			Str
*	Return:
*		No
***********************************************************************************/
void Trim(char * Str)
{
	char * buf;
	int len;
	int begin,end;
	int i;

	if(Str==NULL)
		return;

	len=strlen(Str);
	if(len<=0)
		return;
	
	buf=Str;

	begin=0;end=len-1;
	while((buf[begin]==' ')||(buf[begin]=='\t')) begin++;
	while((buf[end]==' ')||(buf[end]=='\t')) end--;
	if(end<begin)
	{
		Str[0]='\0';
		return;
	}
	
	if(begin==0)
	{
		Str[end+1]='\0';
		return;
	}

	for(i=begin;i<=end;i++)
		Str[i-begin]=buf[i];
	Str[i-begin]='\0';
	return;
}

/**********************************************************************************
*	Function:
*		1. If there exists more than 2 spaces between words in 'Str', remove all
*			but only leave one, notes thar regard Tab ('\t') as space.
*		2. Remove all spaces (' ') and Tab ('\t') in the end and in the
*			beginning of 'Str'
*	Arguments:
*		IN	:
*			Str
*		OUT	:
*			Str
*	Return:
*		No
***********************************************************************************/
void RemoveRedundantSpace(char * Str)
{
	char * buf;
	int len;
	int begin,end;
	int bufcur,strcur;
	int InSpace;

	if(Str==NULL)
		return;

	len=strlen(Str);
	if(len<=0)
		return;
	
	buf=Str;

	begin=0;end=len-1;
	while((buf[begin]==' ')||(buf[begin]=='\t')) begin++;
	while((buf[end]==' ')||(buf[end]=='\t')) end--;
	if(end<begin)
	{
		Str[0]='\0';
		return;
	}

	bufcur=0; strcur=begin;
	InSpace=0;
	while(strcur<=end)
	{
		if((buf[strcur]==' ')||(buf[strcur]=='\t'))
		{
			if(InSpace==1)
			{
				strcur++;
				continue;
			}
			else
			{
				InSpace=1;
				Str[bufcur]=' ';
				strcur++;
				bufcur++;
				continue;
			}
		}
		else
		{
			InSpace=0;
			Str[bufcur]=buf[strcur];
			strcur++;
			bufcur++;
			continue;
		}
	}
	Str[bufcur]='\0';
	return;
}

/**********************************************************************************
*	Function:
*		1. Change all alphabet to upper case in 'Str'
*	Arguments:
*		IN	:
*			Str
*		OUT	:
*			Str
*	Return:
*		No
***********************************************************************************/
void ToUpper(char * Str)
{
	int len;
	int i;

	if(Str==NULL)
		return;

	len=strlen(Str);
	if(len<=0)
		return;
	for(i=0;i<len;i++)
	{
		if((Str[i]>='a')&&(Str[i]<='z'))
			Str[i]='A'+Str[i]-'a';
	}
	return;
}

/**********************************************************************************
*	Function:
*		1. Change all alphabet to lower case in 'Str'
*	Arguments:
*		IN	:
*			Str
*		OUT	:
*			Str
*	Return:
*		No
***********************************************************************************/
void ToLower(char * Str)
{
	int len;
	int i;

	if(Str==NULL)
		return;

	len=strlen(Str);
	if(len<=0)
		return;
	for(i=0;i<len;i++)
	{
		if((Str[i]>='A')&&(Str[i]<='Z'))
			Str[i]='a'+Str[i]-'A';
	}
	return;
}

/**********************************************************************************
*	Function:
*		1. Replace the first 'oldstr' with 'newstr' in 'srcstr'
*	Arguments:
*		IN	:
*			srcstr
*			oldstr
*			newstr
*		OUT	:
*			srcstr
*	Return:
*		1. If find and replace one 'oldstr' with 'newstr' in 'srcstr', return 1
*		2. If find no 'oldstr' in 'srcstr', return 0
*		3. If error (malloc return NULL) return -1
*   Notes:
*       1. srcstr should be large size enough.
***********************************************************************************/
int StrReplace(char * srcstr, char * oldstr, char * newstr)
{
	char * tmpbuffer;
	int prelen,postlen,totallen, newlen,oldlen;
	char * ptr;
	char * tmpchar;

	tmpchar=strstr(srcstr,oldstr);
	if(tmpchar==NULL)
		return 0;

	totallen=strlen(srcstr);
	oldlen=strlen(oldstr);
	newlen=strlen(newstr);

	prelen=tmpchar-srcstr;
	postlen=totallen-prelen-oldlen;
	tmpchar+=oldlen;

	tmpbuffer=(char*)malloc(prelen+newlen+postlen+1);
	if(tmpbuffer==NULL)
		return -1;

	ptr=tmpbuffer;
	memcpy(ptr,srcstr,prelen);
	ptr+=prelen;
	memcpy(ptr,newstr,newlen);
	ptr+=newlen;
	memcpy(ptr,tmpchar,postlen);

	tmpbuffer[prelen+newlen+postlen]=0;
	strcpy(srcstr,tmpbuffer);
	srcstr[prelen+newlen+postlen]=0;
	free((void*)tmpbuffer);
	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -