计算字符串长度、字符串赋值

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014483177/article/details/51418070

以下全部讨论char,wchar_t的请自行查阅msdn.以下函数的更详细信息也请查阅msdn

一:计算字符串长度

1.sizeof:

        sizeof  unary-expression
	sizeof  (type-name)
示例代码:
	int a[]={0,1,2......};
	char b[5]="";
	int arraySize=sizeof(a)/sizeof(a[0])or sizeof(a)/sizeof(int);
	sizeof(b);

结果:
    数组的长度和字符串相应的长度
说明:
    计算字符串时包含空字符位置


2. strlen:

	size_t strlen(
	   const char *str
	);
	str:Null-terminated string.

示例代码:
        char source[100] = "abcdefghijk";
	char des[8];
	int length1 = strlen(source);
	int length2 = strlen(des);

结果:
    length1=11;length2=27(此处值不稳定)
说明:
    此处des没有被初始化,顾不正常.如果在声明des后加上memset(des,0,8);或者char des[8]={0}or=""or="\0"
length2=0;此时正常.
    如果声明为char des[8]={1};此时length2=1,但输出des结果不是1,可自行测试.
此函数计算字符串长度时不包含空字符位置
注意:
    char des[8]='\0'这么写的话,不是初始化


3. strnlen、strnlen_s:

	size_t strnlen(
	   const char *str,
	   size_t numberOfElements 
	);
	size_t strnlen_s(
	   const char *str,
	   size_t numberOfElements 
	);
示例代码:
        char source[100] = "abcdefghijk";
	int length=strnlen_s(source,num);

结果:
    length=num>strlen(source)?strlen(source):num;
    (strlen(source)=11)
注意:
    strnlen is not a replacement for strlen; strnlen is intended to be used only to calculate the size of incoming untrusted data in a buffer of known size—for example, a network packet. strnlen calculates the length but doesn't walk past the end of the buffer if the string is unterminated. For other situations, use strlen.


二:字符串赋值

1.strcpy_s

	errno_t strcpy_s(
	   char *strDestination,
	   size_t numberOfElements,
	   const char *strSource 
	);
	strDestination:Location of the destination string buffer.
	numberOfElements:Size of the destination string buffer in char units for narrow and multi-byte functions, and wchar_t 
                         units for wide functions.
	strSource:Null-terminated source string buffer.
示例代码:
	char source[] = "abcdefghijk";
	char des[x] ="";
	strcpy_s(des+posDes,numberOfElements,source+posSou);

结果:
    自测
说明:
    source的起始位置是source[posSou],des的起始位置是des[posDes]
    1<=numberOfElements<=_countof(source)-posSou<=_countof(des)-posDes
     _countof为计算静态数组中元素数目的宏,包含空字符,此处跟sizeof的计算结果一样


2.sprintf_s

	int sprintf_s(
	   char *buffer,
	   size_t sizeOfBuffer,
	   const char *format,
	   ... 
	);
	buffer:Storage location for output
	sizeOfBuffer:Maximum number of characters to store.
	format:Format-control string...Optional arguments to be formatted
	
示例代码:
	char source[12] = "abcdefghijk";
	char des[10]="" ;
	sprintf_s(des,sizeOfBuffer,"%s",source);

结果:
    报错
说明:
    此函数不够安全,可用_snprintf_s


3._snprintf_s

	int _snprintf_s(
	   char *buffer,
	   size_t sizeOfBuffer,
	   size_t count,
	   const char *format [,
	   argument] ... 
	);
	buffer:Storage location for the output.
	sizeOfBuffer:The size of the storage location for output.Size in bytes for _snprintf_s or size in words for _snwprintf_s.
	Count:Maximum number of characters to store, or _TRUNCATE.
	format:Format-control string
	argument:Optional arguments.
示例代码:
	char source[] = "abcdefghijk";
	char des[x]="" ;
	_snprintf_s(des+pos,sizeOfBuffer,count,"123%s",source);

结果:
    自测
说明:
    1<=sizeOfBuffer<=_countof(des)-pos
    最多可拷贝count个字符到des中,起始位置为des[pos],count不含空字符位置,count<=sizeOfBuffer-pos-1
注意:
    des[pos]前面的字符要被赋值,否则输出des为空,如果des没有被初始化,des[pos]前的字符为乱码.


猜你喜欢

转载自blog.csdn.net/u014483177/article/details/51418070