文章目录
前言
c语言中对字符和字符串的处理是很频繁,但是C语言本身是没有字符串类型的,字符串通常放在常量字符串中或者字符数组中。
字符串常量适用于那些对它不做修改的字符串函数。
一、函数介绍
1.1 strlen
size_t strlen ( const char * str );
- 字符串以’\0’作为结束标志,strlen函数返回的是在字符串’\0’前面出现的字符个数(不包含’\0’)。
- 参数指向的字符串必须要以’\0’结束。
- 注意函数的返回值为size_t, 是无符号的
#include <stdio.h>
#include<string.h>
int main()
{
const char*str1 = "abcdef";
const char*str2 = {
'a','b','c'};
int a=strlen(str1);
int b=strlen(str2);//这里的值是随机值,因为没有遇到'\0'
pirntf("a=%d b=%d\n",a,b);
return 0;
}
1.2 strcpy
char* strcpy(char * destination, const char * source );
- Copies the C string pointed by source into the array pointed by destination, including the
terminating null character (and stopping at that point). - 源字符串必须以 ‘\0’ 结束。
- 会将源字符串中的 ‘\0’ 拷贝到目标空间。
- 目标空间必须足够大,以确保能存放源字符串。
- 目标空间必须可变
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[20]