字符串操作函数
发布时间:2026/9/28 2:18:42来源:尧图网络
目录一.strlen函数二.strcpy函数三.strcat函数四.strcmp函数五.strncpy函数六.strncat函数七.strncmp 函数八.strstr函数九.strtok函数十.strchr函数十一.strrchr函数十二.strpbrk函数十三.strspn函数十四.strcspn函数一.strlen函数size_t strlen( const char *string);头文件string.h作用用于计算字符串长度但不包括 “ \0 ”。示例#includestdio.h #includestring.h int main() { char a[] hello world!; printf(%zd, strlen(a)); return 0; }运行结果如下所示注参数所指向的字符串必须以“\0”结尾。二.strcpy函数char*strcpy(char*strDestination,constchar*strSource);用法将指针 strSource 所指向的字符串复制到目标字符串中即 strDestination 所指向的字符串 中。包括 “ \0 ”返回值复制后字符串首元素地址即strDestination。示例#includestdio.h #includestring.h int main() { char a[13] abc; char b[] hello world!; strcpy(a, b); printf(%s, a); return 0; }运行结果如下注目标字符串必须空间大小足够大确保放得下源字符串。 源字符串必须以“ \0 ”结尾。 目标空间必须可修改。如不能被const修饰。三.strcat函数char*strcat(char*strDestination,constchar*strSource);用法在目标字符串strDestination后面追加上另一个字符串源字符串 strSource。返回值返回拼接后字符串首元素的地址即strDestination。示例#includestdio.h #includestring.h int main() { char a[13] hello ; char b[] world!; strcat(a, b); printf(%s, a); return 0; }运行结果如下所示注目标字符串和源字符串都必须以 \0 结尾。 目标字符串空间必须足够大。 目标空间必须可修改。四.strcmp函数intstrcmp(constchar*string1,constchar*string2);作用是C语言中的字符串比较函数用于比较两个字符串的大小。比较原理按照字符串元素顺序逐个比较若两个字符相等则继续比较下一个字符反之则函 数运行结束。返回值当string1 string2 时返回 -1。 当string1 string2 时 返回0。 当string1 string2 时 返回1。示例#includestdio.h #includestring.h int main() { char a[] abc; char b[] aec; printf(%d, strcmp(a, b)); return 0; }运行结果如下所示注字符串的比较是按照 ascii 码进行比较的因此大小写字母的比较结果不同。 字符串均需以\0进行结尾。五.strncpy函数char *strncpy( char *strDest, const char *strSource, size_tcount);作用将 strSource 所指向的源字符串中 count 个字符拷贝到 strDest 所指向的字符串中。 若 strSource 所指向的源字符串中字符数量不足 count 个将会追加 0。返回值返回结果字符串首元素的地址即strDest。示例#includestdio.h #includestring.h int main() { char a[15] abc; char b[] hello world!; strncpy(a, b, sizeof(b)); printf(%s, a); return 0; }运行结果如下所示注目标字符串和源字符串中间不能有重叠部分目标字符串空间必须足够大。六.strncat函数char*strncat(char*strDest,constchar*strSource,size_tcount);用法将源字符串strSource所指向的字符串中 count 个字符串附加在目标字符串strDest所 指向的字符串末尾。返回值返回目标字符串首元素地址即strDest。示例#includestdio.h #includestring.h int main() { char a[15] hello ; char b[] world!; strncat(a, b, sizeof(b)); printf(%s, a); return 0; }运行结果如下所示七.strncmp 函数intstrncmp(constchar*string1,constchar*string2,size_tcount);用法按顺序一次比较字符串中字符大小直到出现大小不同的字符或比较完 count 个字符。返回值当string1 string2 时返回 -1。 当string1 string2 时 返回0。 当string1 string2 时 返回1。示例#includestdio.h #includestring.h int main() { char a[] abcd; char b[] aec; printf(%d, strncmp(a, b, sizeof(b))); return 0; }运行结果如下所示八.strstr函数char*strstr(constchar*string,constchar*strCharSet);用法用于在 string 所指向的字符串中查找 strCharSet 所指向的字符串并返回第一次出现时的 首元素地址。返回值当 string 所指向的字符串存在所查找的字符串时返回第一次出现时的首元素地址否 则返回NULL。示例#includestdio.h #includestring.h int main() { char a[] hello world!; printf(%s, strstr(a, or)); return 0; }运行结果如下所示九.strtok函数char*strtok(char*strToken,constchar*strDelimit);作用将 strToken 指向的目标字符串的内容按照 strDelimit 所指向的字符串内容进行分割实际 将与strDelimit所指向字符串元素相同的转化为\0记录并返回分割后的字符串首元素地 址当第二次调用该函数并且目标字符串地址为NULL时将默认为上一个非NULL的地 址并返回下一个分割段。返回值返回原字符串单段首元素地址。示例#includestdio.h #includestring.h int main() { char a[] hello world!; char b[] eo; char* c strtok(a, b); while (c ! NULL) { printf(%s\n, c); c strtok(NULL, b); } return 0; }运行结果如下注该函数会改变目标字符串的内容 目标字符串必须可修改 strtok会自动忽略字符串中第一个字符十.strchr函数char*strchr(constchar*string,intc);用法查找 string 所指向的字符串是否有ASCII码值等于c的字符。返回值查找到字符时返回字符的地址未查找到字符时返回 NULL。示例#includestdio.h #includestring.h int main() { char a[] helalo woarld!; char* c strchr(a, 97); printf(%s\n, c); return 0; }运行结果如下所示注strchr只能查找单个字符并且只能查找到第一个匹配项。 不支持多字节字符的查找。十一.strrchr函数char*strrchr(constchar*string,intc);用法反向查找 string 所指向的字符串是否有ASCII码值等于c的字符。返回值查找到字符时返回字符的地址未查找到字符时返回 NULL。示例#includestdio.h #includestring.h int main() { char a[] helalo woarld!; char* c strrchr(a, 97); printf(%s, c); return 0; }运行结果如下所示注strrchr只能查找单个字符并且只能查找到第一个匹配项。 不支持多字节字符的查找。十二.strpbrk函数char*strpbrk(constchar*string,constchar*strCharSet);用法在 string 所指向的字符串中查找 strCharSet 所指向字符串中任意字符并返回第一个符合 的字符地址。返回值查找到字符时返回字符的地址未查找到字符时返回 NULL。示例#includestdio.h #includestring.h int main() { char a[] hello world!; char b[] do; char* c strpbrk(a, b); printf(%s, c); return 0; }运行结果如下十三.strspn函数size_tstrspn(constchar*string,constchar*strCharSet);用法从头开始查找 string 所指向的字符串中属于 strCharSet 的字符并记录直到遇到不属于 strCharSet 中的字符。返回值返回开头到遇到不属于 strCharSet 所指向字符串中字符时所经过的字符个数。示例#includestdio.h #includestring.h int main() { char a[] hello world!; char b[] helowrd! ; printf(%zd, strspn(a, b)); return 0; }运行结果如下所示十四.strcspn函数size_tstrcspn(constchar*string,constchar*strCharSet);用法从头开始查找 string 所指向的字符串中属于 strCharSet 的字符并记录直到遇到属于 strCharSet 中的字符。返回值返回开头到第一个相同元素之间的字符个数。示例#includestdio.h #includestring.h int main() { char a[] hello world!; char b[] oz; printf(%zd, strcspn(a, b)); return 0; }运行记过如下
网站建设高端定制企业官网