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

以下全部讨论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]前的字符为乱码.


### 回答1: 在 C 语言中,要计算字符串的长度,可以使用库函数 `strlen()`。该函数的原型如下: ``` size_t strlen(const char *str); ``` 其中,`str` 指向要求长度的字符串。函数返回值为 `size_t` 类型,表示字符串的长度,单位为字符。 举个例子,如果要计算字符串 "Hello, world!" 的长度,可以这样写: ``` #include <stdio.h> #include <string.h> int main() { char str[] = "Hello, world!"; printf("字符串的长度为 %zu 个字符\n", strlen(str)); return 0; } ``` 输出结果为: ``` 字符串的长度为 13 个字符 ``` 注意,`strlen()` 函数不包括字符串的结尾符 `'\0'` 在内。 ### 回答2: 在C语言中,计算字符串的长度是非常常见的操作。C语言中的字符串是一串字符的数组,以Null字符 '\0' 结尾。 要计算字符串的长度,一种常用的方法是遍历字符串中的每个字符,直到遇到 Null 字符为止。下面是一个简单的示例代码: ``` #include <stdio.h> int main() { char str[] = "Hello, World!"; int len = 0; // 遍历字符串,直到遇到 Null 字符 while (str[len] != '\0') { len++; } printf("字符串的长度为: %d\n", len); return 0; } ``` 在上面的代码中,我们初始化了一个字符串 `str`,然后使用 `while` 循环遍历字符串,每次循环检查当前字符是否为 Null 字符,如果不是,则增加长度计数器 `len` 的值。最后,我们打印输出字符串的长度。 对于上述示例代码中的字符串 "Hello, World!",输出将是:字符串的长度为: 13。 以上就是使用C语言计算字符串长度的方法。 ### 回答3: C语言中,我们可以通过使用库函数strlen()来计算字符串的长度。strlen()函数的原型定义在<string.h>头文件中,它接受一个字符串作为参数,并返回该字符串的长度。 下面是一个例子,展示了如何使用strlen()函数计算字符串长度: ```c #include <stdio.h> #include <string.h> int main() { char str[] = "Hello World!"; int length = strlen(str); printf("字符串的长度是:%d\n", length); return 0; } ``` 在上面的例子中,我们定义了一个字符串变量str,并赋值为"Hello World!"。然后,我们通过调用strlen()函数计算了该字符串的长度,并将结果赋给变量length。最后,我们使用printf()函数打印结果。 当我们运行上面的代码时,控制台将输出:"字符串的长度是:12",表明字符串"Hello World!"的长度为12个字符。 需要注意的是,strlen()函数返回的长度并不包括字符串末尾的空字符'\0'。这意味着,如果一个字符串的长度为n,它实际占用的字符空间将是n+1。 通过使用strlen()函数,我们可以方便地计算字符串的长度,以便在程序中进行相应的处理和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值