String commonly used functions and operations [to be added]

table of Contents

strcpy

strlen

Keyword sizeof

memmove

Remove the spaces at the left and right ends of the string

strcpy

char * strcpy( char * dst, const char * src );
  • strcpy is a standard library function of the C language. strcpy copies the string starting from the src address and containing the'\0' terminator to the address space starting with dest, and the return value type is char*.
  • Return value: the address of the target string.
char* strcpy(char* des,const char* source)
{
	char* r=des;  
	assert((des != NULL) && (source != NULL));
	while((*r++ = *source++)!='\0');
	return des; 
}

strlen

  • strlen is a function, it is used to calculate the length of the specified string str, but does not include the end character (ie the null character)
size_t strlen(char const* str);
  • It is important to note that the function strlen returns a value of type size_t .
/*判断一*/
if(strlen(x)>= strlen(y))
{
}
/*判断二*/
if(strlen(x)- strlen(y)>= 0)   //相减返回值是size_t
{
    
    
  • On the surface, the above two judgment expressions are completely equal, but the actual situation is not the case. Among them, there is no problem with judging expression one, and the program can work exactly as expected; but the result of judging expression two is different, it will always be true
  • The return result of the function strlen is of type size_t (ie unsigned integer), and the type of size_t can never be negative. The statement "if(strlen(x)-strlen(y)>=0)" will always be true.
  • The expression contains both signed and unsigned integers, but unexpected results may still be produced
/*判断一*/
if(strlen(x)>= 5)
{
}
/*判断二*/
if(strlen(x)- 5>=0)
{
}
  • To determine whether the result of expression 2 is always true, the reason is the same as above

Keyword sizeof

  • The keyword sizeof is a unary operator , not a function. Unlike the function strlen, its parameters can be arrays, pointers, types, objects, functions, etc., as shown in the following sample code:
har sArr[] = "ILOVEC";
/*用sizeof求长度*/
printf("sArr的长度=%d\n", sizeof(sArr));
  • Compared with the function strlen, the running result of the sample code here is 7 (because it includes the ending character null ).
  • At the same time, for sizeof, because the buffer has been initialized with a known string and its length is fixed, sizeof calculates the length of the buffer at compile time .
  • It is also calculated at compile time, so sizeof cannot be used to return the size of the dynamically allocated memory space.

memmove

void *memmove(void *dest, const void *src, unsigned int count);

Remove the spaces at the left and right ends of the string

  • Truncate the trailing spaces of the string
void Rtrim(char *string)   
{   
	size_t len = 0;   
	if(string == NULL)   
		return;   

	len = strlen(string);
	while(len > 0 && string[len-1] == ' ')   //位置换一下   
		string[--len] = 0;   
	return;   
}
  • Intercept the first space of the string
void Ltrim(char *string)
{
	size_t len = 0;
	len = strlen(string);   
	char *p_tmp = string;

	//不是以空格开头
	if( (*p_tmp) != ' ') 
		return;
	
	//找第一个不为空格的
	while((*p_tmp) != '\0')
	{
		if( (*p_tmp) == ' ')
			p_tmp++;
		else
			break;
	}

	//全是空格(从上面已经遍历到最后一个,指针仍然指向空格,则字符串全为空)
	if((*p_tmp) == '\0') 
	{
		*string = '\0';
		return;
	}

	//不全是空格(p_tmp2指向字符串头部,两个指针,将p_tmp指向的非空区域拷贝到前面去)
	//也可以考虑使用memmove(p_tmp2,p_tmp,ncount); ncount可以通过 len - (p_tmp - p_tmp2) 得出
	char *p_tmp2 = string; 
	while((*p_tmp) != '\0')
	{
		(*p_tmp2) = (*p_tmp);
		p_tmp++;
		p_tmp2++;
	}
	(*p_tmp2) = '\0';
    return;
}

 

Guess you like

Origin blog.csdn.net/baidu_41388533/article/details/114850398