网站建设的发展历史与新方向,wordpress二次开发函数,wordpress留言标签,logo设计的最好的公司各位CSDN的uu们你们好呀#xff0c;今天小雅兰来给大家介绍一个全新的知识点#xff0c;就是字符函数和字符串函数啦#xff0c;其实其中有些函数我之前已经学习过了#xff0c;比如strlen、strcpy#xff1b;也有一些之前不是很熟悉的函数#xff0c;比如strstr、strtok… 各位CSDN的uu们你们好呀今天小雅兰来给大家介绍一个全新的知识点就是字符函数和字符串函数啦其实其中有些函数我之前已经学习过了比如strlen、strcpy也有一些之前不是很熟悉的函数比如strstr、strtok、strerror等等。话不多说啦现在让我们进入字符函数和字符串函数的世界吧 求字符串长度 strlen
长度不受限制的字符串函数 strcpy strcat strcmp
长度受限制的字符串函数介绍 strncpy strncat strncmp C语言中对字符和字符串的处理很是频繁但是C语言本身是没有字符串类型的字符串通常放在
常量字符串中或者字符数组中。
字符串常量适用于那些对它不做修改的字符串函数. strlen size_t strlen ( const char * str ); 字符串已经 \0 作为结束标志strlen函数返回的是在字符串中 \0 前面出现的字符个数不包含 \0 )。 #define _CRT_SECURE_NO_WARNINGS 1
#includestdio.h
#includestring.h
int main()
{char arr[] abc\0def;int len strlen(arr);printf(%d\n, len);return 0;
} #define _CRT_SECURE_NO_WARNINGS 1
#includestdio.h
#includestring.h
int main()
{char arr[] abcdef;int len strlen(arr);printf(%d\n, len);return 0;
} abcdef后面隐藏了一个\0 参数指向的字符串必须要以 \0 结束。 #define _CRT_SECURE_NO_WARNINGS 1
#includestdio.h
#includestring.h
int main()
{char arr[3] { a,b,c};int len strlen(arr);printf(%d\n, len);return 0;
} 如果字符串不以\0结束那么结果就是一个随机值 注意函数的返回值为size_t是无符号的 #define _CRT_SECURE_NO_WARNINGS 1
#includestdio.h
#includestring.h
int main()
{const char* str1 abcdef;const char* str2 bbb;if (strlen(str2) - strlen(str1) 0){printf(str2str1\n);}else{printf(srt1str2\n);}return 0;
} 模拟实现strlen 三种方式 1.计数器的方式 2.递归的方式 3.指针-指针的方式 函数递归青蛙跳台阶——“C”_认真学习的小雅兰.的博客-CSDN博客 指针——“C”_认真学习的小雅兰.的博客-CSDN博客 1.计数器的方式 #define _CRT_SECURE_NO_WARNINGS 1
#includestdio.h
#includeassert.h
int my_strlen(const char* str)
{//计数器的方式int count 0;assert(str ! NULL);while (*str ! \0){str;count;}return count;
}
int main()
{char arr[] abcdef;int len my_strlen(arr);printf(%d\n, len);return 0;
} 2.递归的方式 #define _CRT_SECURE_NO_WARNINGS 1
#includestdio.h
//不能创建临时变量,求字符串的长度
int my_strlen(const char * str)
{if(*str\0)return 0;elsereturn 1 my_strlen(str1);
}int main()
{char arr[] abcdef;int len my_strlen(arr);printf(%d\n, len);return 0;
} 3.指针-指针的方式 #define _CRT_SECURE_NO_WARNINGS 1
#includestdio.h
//指针-指针的方式
int my_strlen(char * s)
{char * p s;while( * p ! \0)p;return p-s;
}int main()
{char arr[] abcdef;int len my_strlen(arr);printf(%d\n, len);return 0;
} 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 结束。 #includestdio.h
#includestring.h
int main()
{char arr1[3] { a,b,c };char arr2[20] xxxxxx;strcpy(arr2, arr1);printf(%s\n, arr2);return 0;
} 这样程序直接崩溃了 会将源字符串中的 \0 拷贝到目标空间。 目标空间必须足够大以确保能存放源字符串。 #includestdio.h
#includestring.h
int main()
{//错误的示范char arr1[20] abcdefghi;char arr2[3] ;strcpy(arr2, arr1);printf(%s\n, arr2);return 0;
} 目标空间必须可变。 #includestdio.h
#includestring.h
int main()
{//错误的示范char* p abcdefghi;char arr2[20] hehe;strcpy(p, arr2);printf(%s\n, p);return 0;
} 模拟实现strcpy #includestdio.h
#includestring.h
//1.参数顺序
//2.函数的功能停止条件
//3.assert
//4.const修饰指针
//5.函数返回值
//6.题目出自《高质量C/C编程》书籍最后的试题部分
//返回的是目标空间的起始地址
#includeassert.h
char * my_strcpy(char * dest, const char* src)
{char * ret dest;assert(dest!NULL);assert(src ! NULL);while ((*dest *src)){;}return ret;
}
int main()
{char arr1[] hehe;char arr2[20] { 0 };my_strcpy(arr2, arr1);printf(%s\n, arr2);return 0;
} strcat char * strcat ( char * destination, const char * source ); #includestdio.h
#includestring.h
int main()
{char arr1[20] hello ;char arr2[] world;//追加strcat(arr1, arr2);printf(%s\n, arr1);return 0;
} Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination. 源字符串必须以 \0 结束。 #includestdio.h
#includestring.h
int main()
{char arr1[20] hello \0xxxxxxxxxxxx;char arr2[] world;//追加strcat(arr1, arr2);printf(%s\n, arr1);return 0;
}目标空间必须有足够的大能容纳下源字符串的内容。 目标空间必须可修改。 模拟实现strcat #includestdio.h
#includeassert.h
char* my_strcat(char* dest, const char* src)
{char* ret dest;assert(dest ! NULL);assert(src ! NULL);//找目标空间的\0while (*dest!\0){dest;}//拷贝while ((*dest *src)){;}return ret;
}
int main()
{char arr1[20] hello ;char arr2[] world;//追加my_strcat(arr1, arr2);printf(%s\n, arr1);return 0;
} 绝对不能自己给自己追加 #includestdio.h
#includeassert.h
char* my_strcat(char* dest, const char* src)
{char* ret dest;assert(dest ! NULL);assert(src ! NULL);//找目标空间的\0while (*dest ! \0){dest;}//拷贝while ((*dest *src)){;}return ret;
}
int main()
{char arr1[20] bit;//追加my_strcat(arr1, arr1);printf(%s\n, arr1);return 0;
} strcmp “abcdefbbcdef这里比较的是两个字符串首字符的地址而并不是字符串的内容 比较两个字符串内容的时候不能使用应该使用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的数字#includestdio.h
#includestring.h
#includeassert.h
int main()
{char arr1[] abcdef;char arr2[] bbcdef;int ret strcmp(arr1, arr2);printf(%d\n, ret);return 0;
} 模拟实现strcmp #includestdio.h
#includestring.h
#includeassert.h
int my_strcmp(const char* str1, const char* str2)
{assert(str1 ! NULL);assert(str2 ! NULL);while (*str1 *str2){if (*str1 \0){return 0;}str1;str2;}if (*str1 *str2){return 1;}else{return -1;}
}int main()
{char arr1[] abcdef;char arr2[] bbcdef;int ret my_strcmp(arr1, arr2);printf(%d\n, ret);return 0;
}另一种写法 #includestdio.h
#includestring.h
#includeassert.h
int my_strcmp(const char* str1, const char* str2)
{assert(str1 ! NULL);assert(str2 ! NULL);while (*str1 *str2){if (*str1 \0){return 0;}str1;str2;}return *str1 - *str2;
}
int main()
{char arr1[] abcdef;char arr2[] bbcdef;int ret my_strcmp(arr1, arr2);printf(%d\n, ret);return 0;
}strncpy char * strncpy ( char * destination, const char * source, size_t num ); Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it. 拷贝num个字符从源字符串到目标空间。 如果源字符串的长度小于num则拷贝完源字符串之后在目标的后边追加0直到num个。 #includestdio.h
#includestring.h
#includeassert.h
int main()
{char arr1[] abcdef;char arr2[5] { 0 };strncpy(arr2, arr1, 3);printf(%s\n, arr2);return 0;
} strncat char * strncat ( char * destination, const char * source, size_t num ); Appends the first num characters of source to destination, plus a terminating null-character.If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.#includestdio.h
#includestring.h
#includeassert.h
int main()
{char arr1[20] hello \0xxxxxxxx;char arr2[] world;strncat(arr1, arr2, 3);printf(%s\n, arr1);return 0;
} strncmp int strncmp ( const char * str1, const char * str2, size_t num ); 比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。 #includestdio.h
#includestring.h
#includeassert.h
int main()
{char arr1[] abcdef;char arr2[] abcq;int ret strncmp(arr1, arr2, 4);printf(%d\n, ret);return 0;
} 好啦小雅兰今天的内容就到这里啦还要继续加油呀