字符串的使用和剖析
函数介绍
一、strlen()
size_t strlen(const char *str);
- 字符串已经’\0’作为结束标志,strlen()函数返回的是在字符串中’\0’前面出现的字符个数(不包含’\0’)。
- 参数指向的字符串必须要以’\0’结束。
- 注意函数的返回值为size_t,是无符号的(易错)。
//size_t == unsigned int
//两者表达等价
#include<stdio.h>
#include<string.h>
int main()
{
//int len = strlen("abcdef");
//错误示范
//char arr[] = {'a','b','c','d','e','f'};
//int len = strlen(arr);
//printf("%d\n",len);
if(strlen("abc") - strlen("abcdef") > 0)
{
printf("hehe\n");//无符号型-无符号型=无符号型
//size_t 3 - size_t 6 = size_t 3 > 0
}
else
{
printf("haha\n");
}
return 0;
}
因此有时我们使用size_t类型可以防止得到的结果为负数
二、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 that point).
- 源字符串必须以’\0’结束
- 会将源字符串中的‘\0’拷贝到目标空间
- 目标空间必须足够大,以确保能存放源字符串
- 目标空间必须可变
- 模拟实现
myStrcopy(char *dest,char*src)
{
assert(dest != NULL);
assert(src != NULL);
while (*src != '\0')
{
*dest = *src;//源字符串首字符替换目标空间首元素
++dest;
++src;;
}
*dest = *src;//当遇到'\0'时,继续copy
}
进一步精简代码
#include<assert.h>
char* myStrcpy(char* dest,const char* src)
{
assert(dest != NULL);
assert(src != NULL);
char* ret = dest;
//拷贝src指向的字符串到dest指向的空间,包含'\0'
while(*dest++ = *src++)
{
;
}
//返回目的地的起始地址
return ret;
}
注意事项:错误示范
int main()
{
char arr1[] = "abcdefghi";
char arr2[] = {'a', 'b', 'c'};
strcopy(arr1,arr2);
printf("%s\n",arr1);
return 0;
}
由于arr2[]没有‘\0’,所以strcopy()不确定在哪停止。
三、strcat
char *strcat(char *destination, const char *source);
- Appends a copy of the source String to the destination String. The terminating null character in destination is overwrritten by the first character of source, and a null-character is inlcuded at the end of the new string formed by the concatenation of both in destination.
- 源字符串必须以’\0’结束
- 目标空间必须足够大
- 目标空间必须可修改
- 字符串不能自己给自己追加
#define _CRT_SECURE_NO_WARNINGS//忽视vs的安全警告
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[30] = "Hello\0xxxxxxxx";
char arr2[] = "world";
strcat(arr1,arr2);
printf("%s\n", arr1);
return 0;
}
#include<assert.h>
char *myStrcat(char* dest, const char* src)
{
char *ret =dest;
assert(dest !=NULL);
assert(src);
//1.找到目的字符串的'\0'
while (*dest != '\0')
{
dest++;
}
//2.追加
{
while (*dest++ = *src++)
{
;
}
return ret;
}
四、strcmp()
int strcmp(const char *str1,const char *str2);
- This function starts comparing the first character of each string. if they are equal to each other.it continues with the following pairs until the characters differ or until a terminating null-character is reached.
- 标准规定
- 第一个字符串大于第二个字符串,则返回大于0的数字
- 第一个字符串等于第二个字符串,则返回0
- 第一个字符串小于第二个字符串,则返回小于0的数字
-那么如何判断的呢?
int main()
{
//vs
//>1
//==0
//<-1
//linux-gcc
//> >0
//== 0
//< <0
char* p1 ="qbc";//6
char *p2 ="abc";//5
//int ret = strcmp(p1,p2);
if(strcmp(p1,p2) == 1)
{
printf("p1>p2\n");
}
else if(strcmp(p1,p2) ==0)
{
printf("p1 == p2\n");
}
else
{
printf("p1 < p2\n");
}
}
strcmp()函数实现
int myStrcmp(const char* str1,const char* str2)
{
assert(str1 && str2);
//比较
while(*str1 == *str2)//比较的是ASCII码大小
{
if(*str1 == '\0')
{
return 0;//相等
}
str1++;
str2++;
}
if(*str1 > *str2)
return 1;//大于
else
return -1;//小于
}
五、strncpy()
相比于strcpy()多了个n
int main()
{
char arr1[10] = "abcdefgh";
char arr2[] = "hello";
strncpy(arr1,arr2,7);//相比于strcpy()这里多了个 7 参数
return 0;
}
可以看到hello不够7位,补两位‘\0’
strncopy()库函数代码
char *_cdecl strncpy(
char *dest;
const char *source;
size_t count;
)
{
char *start = dest;
while(count && (*dest++ = *source++))//copy string
count--;
if(count)// pad out with zeros
while(--count)
*dest++ = '\0';
return(start);
}
五、strncat()
//int main()
//{
//char arr1[10] = "abcdefgh";
//char arr2[] ="bit";
//strncopy--模拟实现
//strncopy(arr1,arr2,6);
return 0;
//}
int main()
{
char arr1[30] = "hello\0xxxxxxx";
char arr[] = "world";
strncat(arr1,arr2,3)'
printf("s\n",arr1);
return 0;
}
从函数内在机理角度来讲:
短:补‘\0’*,给定的长度短于字符串长度,给定n==3,可以看到arr2前三个字符wor被copy到arr1中,并主动补充’\0’
长:不管,给定长度大于字符串长度,则不用主动添加‘\0’,因为这个’\0’,就是从arr2过去的。不知道你们听不听得明白?
strncat库函数实现
char *_cdecl strncat(
char *front;
const char *back;
size_t count;
)
{
char *start = front;
while(*front++)
;
front--;//找到\0前一个位置
while(count--)
if(!(*front++ = *back++))
return(start);
//如果较短,没有到\0,则主动补充\0
*front = '\0'
return(start);
}
六、strstr()
找子字符串
char *strstr(const char*, const char*);
- Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
/*strstr example*/
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "This is a simple string";
char = pch;
pch = strstr(str,"simple");
strncpy(pch,"simple",6);
puts(str);
return 0;
}
int main()
{
char *p1 = "abcdefabcdef";
char *p2 = "def";
char *ret = strstr(p1,p2);
if (ret == NULL)
{
printf("子串不存在\n");
}
else
{
printf("%s\n", ret);
}
return 0;
}
strstr(),有子串则返回地址,没有则返回空指针
strstr()的实现
char *myStrStr(const char *p1,const *p2)
{
assert(p1);
assert(p2);
char *s1 = NULL;
char *s2 = NULL;
char *cur = p1;
if(*p2 == '\0')
{
return p1;
}
while(*cur)
{
s1 = cur;
s2 = p2;
while((*s1 && *s2 && (*s1 == s2))
{
s1++;
s2++;
}
if(!*s2 )
{
return cur;//找到子串
}
cur++;
}
return NULL;
}
七、strtok()
char *strtok(char *str,const char *sep);
- sep参数是个字符串,定义了用作分隔符的字符集合
- 第一个参数指定一个字符串,它包含了0个或多个由sep字符串中一个或多个分隔符分割的标记
- strtok函数找到str中的下一个标记,并将其用\0结尾,返回一个指向这个标记的指针。(注意:strtok函数会改变被操作的字符串,所以在使用时,一般都使用临时拷贝的内容)
- strtok函数的第一个参数不为NULL,函数找到str中第一个标记,strtok函数将保存它在字符串中的位置。
- strtok函数第一个参数为NULL,函数在同一个字符串中被保存的位置开始,查找下个标记。
strtok()的使用方法
int main()
{
char arr[] = "z136137@qq.com";
char* p = "@.";
char buf[1024] = { 0 };
strcpy(buf, arr);//备份
char* ret = NULL;
for (ret = strtok(arr, p); ret != NULL; ret = strtok(NULL, p))
{
printf("%s\n", ret);
}
}
八、strerror()
char *strerror(int reenum)
返回错误码,所对应的错误信息
#include<stdio.h>
#include<string.h>
#include<errno.h>//必须包含的头文件
int main()
{
//错误码 错误信息
//0- No error
//1- Operation not permitted
//2- No such file or directory
//...
//errno 是一个全局错误码的变量
//当C语言的库函数在执行过程中,发生量错误,就会把对应的错误码,赋值到errno中
char *str = strerrno(errno);
printf("%s\n",str);
return 0;
}
九、其他字符函数