c语言-----------(关于字符串函数)详解
目录
前言:
一,求字符串长度函数(strlen)
二,长度不受限制的字符串函数
2.1复制字符串函数(strcpy)
2.2 字符串连接函数 (strcat)
2.3 字符串比较函数(strcmp)
三、长度受限制的字符串函数
3.1复制字符串函数(strncpy)
3.2 字符串连接函数 (strncat)
3.3 比较字符串函数 (strncmp)
四,字符串查找
4.1查找子字符串函数(strstr)
4.2字符串分割函数(strtok)
前言:
字符串是一种重要的数据类型,但是 C语言没有显式的字符串数据类型,字符串通过字符串常量或字符数组方式储存。C语言提供了许多与字符串相关函数,本章将对字符串相关的函数进行讲解。
一,求字符串长度函数(strlen)
库函数strlen函数声明: size_t strlen ( const char * str );
注意:
(1)字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。
(2)参数指向的字符串必须要以 '\0' 结束。
(3)注意函数的返回值为size_t,是无符号的.
2. strlen的模拟实现:
#include
#includesize_t my_strlen(const char* str)
{assert(str!=NULL); //断言 用来检测str是不是为空 因为空指针不能用来进行解引用的操作int count=0;while(*str!='\0'){count++;str++;}return count;
}void main()
{char str[]="Hello world";printf("%d\n",my_strlen(str));
}
二,长度不受限制的字符串函数
这些字符串函数是“不受限制的”,表示它们只是通过寻找字符串结束标志’\0’来判断字符串长度,在使用这些函数时,程序员必须保证字符串不会溢出所在内存,必须要引入#include
2.1复制字符串函数(strcpy)
功能:将source空间中的字符串拷贝到destination中,包括’\0’,并返回destination字符串的起始地址。
库函数strcpy的函数声明:char* strcpy(char * destination, const char * source );
注意:
(1)源字符串必须以 '\0' 结束。
(2)会将源字符串中的 '\0' 拷贝到目标空间。
(3)目标空间必须足够大,以确保能存放源字符串。
(4)目标空间必须可变。
2.模拟实现:
返回值:返回目标数组起始地址
strDestination:目标数组起始地址
strSource:源字符串起始地址
#include
#includechar* my_strcpy(char* strDestination,const char *strSource)//传递的是目标字符串和拷贝字符串的地址(所要复制的字符串不被改变所以一般用const修饰){
//1.检查参数assert(strDestination!=NULL&&strSource!=NULL);//2.保护参数char* pDest=strDestination;//3.拷贝复制while(*strSource!='\0'){*pDest++=*strSource++;}*pDest='\0';
return strDestination;}void main()
{char str[20]="C++";char *str1="Linux";printf("str=%s\n",str);
printf("str=%s\n",my_strcpy(str,str1));
}
2.2 字符串连接函数 (strcat)
功能:将source指向的字符串追加到destination字符串尾部。destination字符串中的’\0’被source字符串中的第一个字符覆盖,并且在destination字符串中由这两个字符串联而成的新字符串的末尾添加’\0’
库函数strcat的函数声明:char * strcat ( char * destination, const char * source );
注意:
(1)源字符串必须以 '\0' 结束。
(2)目标空间必须有足够的大,能容纳下源字符串的内容。
(3)目标空间必须可修改。
2.模拟实现:
返回值:返回目标字符串起始地址
strDestination:目标字符串起始地址
strSource:要追加的字符串起始地址
char* my_strcat(char *strDestination,const char* strSource)
{//1.检查参数assert(strDestination!=NULL&&strSource!=NULL);//2.保护参数char* pDest=strDestination;//3.寻找末尾while(*pDest!='\0')
{pDest++;
}
//把要连接的字符串连接到目标字符串中while(*strSource!='\0')
{*pDest++=*strSource++;
}*pDest='\0';
return strDestination;}void main()
{char str[20]="Hello";char*str1=" World";printf("str=%s\n",str);printf("str=%s\n",my_strcat(str,str1));
}
2.3 字符串比较函数(strcmp)
功能:对两个字符串进行比较,第一个字符串大于第二个字符串,则返回大于0的数字,第一个字符串等于第二个字符串,则返回0,第一个字符串小于第二个字符串,则返回小于0的数字。
库函数strcmp的函数声明:int strcmp ( const char * str1, const char * str2 );
2.模拟实现:
比较方法:从第一个字符开始比较,当出现不相同时就不会进行后面的比较,如果都相同那就返回0。
int my_strcmp(const char*str1, const char*str2)
{assert(str1 != NULL&&str2 != NULL);int ret = 0;while (*str1!='\0'||*str2!='\0'){ret = *str1 - *str2;if (ret != 0)break;str1++;str2++;}return ret;
}
void main()
{char* str1 = "Hello";char *str2 = "hello";printf("%d", my_strcmp(str1, str2));}
三、长度受限制的字符串函数
标准库包含一些受限制的函数,这些函数接受一个显式的长度参数,用于限定进行复制或比较的字符数,防止难以预料的非法访问内存。必须引入#include
3.1复制字符串函数(strncpy)
库函数strncpy声明char * strncpy ( char * destination, const char * source, size_t num );
功能:从source中拷贝num长度的字符串到destination中。
注意:并不在后面追加'\0',并且只关注拷贝多少个字符。
返回值:返回的是destination字符串的起始地址。
代码演示:
#include
#include
void main()
{char str[] = "Hello world";char *str1 = "good";printf("%s\n", strncpy(str, str1, 3));
}
输出:goolo world
注意:如果所要复制的字符数num超过了str1中字符的个数,那么后面多出的字符就会用'\0'去填充。总共达到num的长度。
void main()
{char str[100] = "Hello world";char *str1 = "good";printf("%s\n", strncpy(str, str1,8));
}
输出:good
2.模拟实现:
#include
#include
char*my_strncpy(char *destination, const char* source, size_t num)
{assert(destination != NULL&&source != NULL);char*pDest = destination;while (num !=0&& *source != '\0'){*pDest++ = *source++;num--;}//表示上面的source已经拷贝完了,但是count还是>0while (num){*pDest++= '\0';num--;}return destination;
}
void main()
{char str[100] = "Hello world";char *str1 = "good";printf("%s\n", my_strncpy(str, str1, 3));
}
3.2 字符串连接函数 (strncat)
库函数strncat声明:char * strncat ( char * destination, const char * source, size_t num );
功能: 从字符串source的开头拷贝n 个字符到destination字符串尾部,并追加’\0’字符。
注意:destination要有足够的空间来容纳要拷贝的字符串。如果num大于字符串source的长度,那么仅将source指向的字符串内容追加到destination的尾部。字符追加完成后,再追加’\0’。
返回值:返回的是destination字符串的起始地址。
模拟实现:
char *my_strncat(char*destination, const char*source,size_t num)
{assert(destination != NULL&&source != NULL);char *pdest = destination;const char*src = source;while (*pdest != '\0'){pdest++;}while (num != 0){*pdest++ = *src++;num--;}*pdest = '\0';return destination;
}void main()
{char str[100] = "Hello world";char *str1 = "good";printf("%s\n", my_strncat(str, str1,3));
}
3.3 比较字符串函数 (strncmp)
库函数strncmp声明:int strncmp ( const char * str1, const char * str2, size_t num );
功能:比较两个字符串,但最多比较num个字节。如果两个字符串前len个字符相等返回0,str1大于str2返回大于0值,str1小于str2返回小于0的值
模拟实现:
int my_strncmp(const char *str1, const char*str2, size_t num)
{assert(str1 != NULL&& str2 != NULL);int res = 0;while (num != 0){if ((res = *str1 - *str2) != 0)break;str1++;str2++;num--;}return res;
}void main()
{char str[100] = "Hello world";char *str1 = "hello world";printf("%d\n", my_strncmp(str, str1,3));
}
四,字符串查找
4.1查找子字符串函数(strstr)
库函数strstr声明:char * strstr ( const char *str1, const char *str2 );
功能:从str1中查找是否存在str2字符串
返回值:如果存在,则返回str2字符串在str1字符串中起始位置,否则返回NULL;
2.模拟实现:
char *my_strstr(const char *str1, const char*str2)
{assert(str1 != NULL&&str2 != NULL);const char*s1 = str1, *s2 = str2;while (*s1 != '/0'){char *tmp1 = s1;char *tmp2 = s2;while (*tmp1 != '\0'&&*tmp2 != '\0'&&*tmp1 == *tmp2){tmp1++;tmp2++;}if (*tmp2 == '\0'){return s1;}s1++;}return NULL;
}int main()
{char *str1 = "hello worlded";char *str2 = "world";printf("%s\n", my_strstr(str1, str2));
}
4.2字符串分割函数(strtok)
库函数strtok声明:char * strtok ( char * str, const char * sep );
功能:字符串分割,将str字符串按照指定的分隔符分割出各个字串
代码演示:
int main ()
{char str[] ="- This, a sample string.";char * pch;printf ("Splitting string \"%s\" into tokens:\n",str);pch = strtok (str," ,.-");while (pch != NULL)
{printf ("%s\n",pch);pch = strtok (NULL, " ,.-");
}return 0;
}
模拟实现:
实现流程:
(1)在传入一个字符串的时候,在字符串中找间隔符,但是若传入的是一个NULL,要从上一次字符串中继续查找分割。
实现:在函数内有一个静态指针保存上一次字符串查找到的间隔符的下一个字符位置。
(2)将间隔符位置替换为\0.
(3)将进入函数时保存的起始地址给返回回去
(4)如果在字符串中没有找到对应的间隔符则返回字符串起始地址
char *my_strtok(char*str, char *sep)
{assert(sep != NULL);static char*ptr; //本次查找的起始地址//如果传入了数据,就代表新的字符串分割//如果没有传入数据,而是传入了NULL,就表示对上一次字符串进行分割if (str != NULL){ptr = str;}if (*ptr == '\0'){return NULL;}char *res = ptr; //保存本次要返回的字串的起始地址while (*ptr != '\0'){char *se = sep;while (*se != '\0'){if (*se == *ptr){//如果start位置就是第一个间隔符,则替换为\0//表示第一个字串到此为止*ptr = '\0';ptr++;if (strlen(res) == 0){break;}return res;}se++;}//se循环到\0表示ptr位置不是间隔符//这位置判断是当遇到连续间隔符的时候,则子串长度为0//并没有返回res,而是要从下一个位置重新开始判断if (*sep != '\0'){res = ptr;continue;}ptr++;}return res;}
int main()
{char str[] = "- This, a sample string.";char * pch;printf("Splitting string \"%s\" i nto tokens:\n", str);pch = my_strtok(str, " ,.-");while (pch != NULL){printf("%s\n", pch);pch = my_strtok(NULL, " ,.-");}return 0;
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

