天天看點

C語言:求字元串長度。

#include<string.h>

size_t strlen ( const char * str );

1、字元串已經 ‘\0’ 作為結束标志,strlen函數傳回的是在字元串中 ‘\0’ 前面出現的字元個數(不包含 ‘\0’ )。

2、參數指向的字元串必須要以 ‘\0’ 結束。

3、注意函數的傳回值為size_t,是無符号整型的( 易錯 )

//模拟實作strlen函數。
#include<assert.h>
int mystrlen(char* str)
{
	int count = 0;
	assert(str != NULL);
	while (*str!='\0')
	{
		count++;
		str++;
	}
	return count;
}
           

繼續閱讀