C语言第二十六弹---字符串函数(下)

✨个人主页:熬夜学编程的小林

💗系列专栏: 【C语言详解】 【数据结构详解】

目录

[1、strncat 函数的使用](#1、strncat 函数的使用)

[2、strncmp 函数的使用](#2、strncmp 函数的使用)

[3、strstr 函数的使用和模拟实现](#3、strstr 函数的使用和模拟实现)

[4、strtok 函数的使用](#4、strtok 函数的使用)

[5、strerror 函数的使用](#5、strerror 函数的使用)

[6、perror 函数的使用](#6、perror 函数的使用)

总结


1、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.
(将source指向字符串的前num个字符追加到destination指向的字符串末尾,再追加⼀个 \0 字符)。
• If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.(如果source 指向的字符串的长度小于num的时候,只会将字符串中到 \0 的内容追加到destination指向的字符串末尾)。

/* strncat example */
#include <stdio.h>
#include <string.h>
int main ()
{
 char str1[20];
 char str2[20];
 strcpy (str1,"To be ");//将"To be "拷贝到str1
 strcpy (str2,"or not to be");//将"or not to be"拷贝到str2
 strncat (str1, str2, 6);//将str2前面6个字符接到str1末尾
 printf("%s\n", str1);//打印str1
 return 0;
}

2、strncmp 函数的使用

int strncmp ( const char * str1, const char * str2, size_t num );

比较str1和str2的前num个字符,如果相等就继续往后比较,最多比较num个字母,如果提前发现不⼀样,就提前结束,大的字符所在的字符串大于另外⼀个。如果num个字符都相等,就是相等返回0.

#include <stdio.h>
#include <string.h>
int main()
{
	char str1[20];
	char str2[20];
	strcpy(str1, "abcdef");//将"abcdef"拷贝到str1
	strcpy(str2, "abc");//将"abc"拷贝到str2
	int ret=strncmp(str1, str2, 4);//将返回值给赋值给ret
	if (ret > 0)
		printf("str1 > str2\n");
	else if (ret < 0)
		printf("str1 < str2\n");
	else
		printf("str1 == str2\n");
	return 0;
}

3、strstr 函数的使用和模拟实现

char * strstr ( const char * str1, const char * str2);

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
(函数返回字符串str2在字符串str1中第⼀次出现的位置)。
The matching process does not include the terminating null-characters, but it stops there.(字符串的比较匹配不包含 \0 字符,以 \0 作为结束标志)。

/* strstr example */
#include <stdio.h>
#include <string.h>
int main ()
{
 char str[] ="This is a simple string";
 char * pch;
 pch = strstr (str,"simple");//找到与simple相等的首地址赋值给pch
 strncpy (pch,"sample",6);//将sample前6个字符拷贝到pch 即修改str中的simple
 printf("%s\n", str);//打印str
 return 0;
}


strstr的模拟实现:

char * strstr (const char * str1, const char * str2)
{
 char *cp = (char *) str1;//str1为const修饰指针,需强制转化为可改指针
 char *s1, *s2;
 if ( !*str2 )//str2为空返回str1首地址
 return((char *)str1);//返回值为char*,需强转成char*
 while (*cp)
 {
 s1 = cp;
 s2 = (char *) str2;
 while ( *s1 && *s2 && !(*s1-*s2) )//*s1 s2不为'\0'且*s1==*s2,
 s1++, s2++;
 
 if (!*s2)//*s2为'\0'则返回cp
 return cp;
 cp++;//否则cp++
 }

 return NULL;//没有找到则返回NULL
}

4、strtok 函数的使用

char * strtok ( char * str, const char * sep);

• sep参数指向⼀个字符串,定义了用作分隔符的字符集合
• 第⼀个参数指定⼀个字符串,它包含了0个或者多个由sep字符串中⼀个或者多个分隔符分割的标记。
• strtok函数找到str中的下⼀个标记,并将其用 \0 结尾,返回⼀个指向这个标记的指针。(注: strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串⼀般都是临时拷贝的内容并且可修改。)
• strtok函数的第⼀个参数不为 NULL ,函数将找到str中第⼀个标记,strtok函数将保存它在字符串中的位置。
• strtok函数的第⼀个参数为 NULL ,函数将在同⼀个字符串中被保存的位置开始,查找下⼀个标记。
• 如果字符串中不存在更多的标记,则返回 NULL 指针。

#include <stdio.h>
#include <string.h>

int main()
{
 char arr[] = "192.168.6.111";
 char* sep = ".";
 char* str = NULL;
 for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep))
 {
 printf("%s\n", str);
 }
 return 0;
}

5、strerror 函数的使用

char * strerror ( int errnum );

strerror函数可以把参数部分错误码对应的错误信息的字符串地址返回来。
在不同的系统和C语⾔标准库的实现中都规定了⼀些错误码,⼀般是放在 errno.h 这个头文件中说明的,C语言程序启动的时候就会使用⼀个全面的变量errno来记录程序的当前错误码,只不过程序启动的时候errno是0,表示没有错误,当我们在使用标准库中的函数的时候发⽣了某种错误,就会讲对应的错误码,存放在errno中,而⼀个错误码的数字是整数很难理解是什么意思,所以每⼀个错误码都是有对应的错误信息的。strerror函数就可以将错误对应的错误信息字符串的地址返回。

#include <errno.h>
#include <string.h>
#include <stdio.h>

//我们打印⼀下0~10这些错误码对应的信息
int main()
{
 int i = 0;
 for (i = 0; i <= 10; i++) {
 printf("%s\n", strerror(i));
 }
 return 0;
}

在Windows11+VS2022环境下输出的结果如下:

No error 没有错误
Operation not permitted 不允许操作
No such file or directory 没有这样的文件或目录
No such process 没有这样的过程
Interrupted function call 中断的函数调用
Input/output error 输入/输出误差
No such device or address 没有这样的设备或地址
Arg list too long 参数列表太长
Exec format errorExec 格式错误
Bad file descriptor 错误的文件描述符
No child processes 无子进程
举例:

#include <stdio.h>
#include <string.h>
#include <errno.h>
int main ()
{
 FILE * pFile;
 pFile = fopen ("unexist.ent","r");
 if (pFile == NULL)
 printf ("Error opening file unexist.ent: %s\n", strerror(errno));
 return 0;
}

输出:

 Error opening file unexist.ent: No such file or directory

也可以了解⼀下perror函数,perror 函数相当于⼀次将上述代码中的第9行完成了,直接将错误信息打印出来。perror函数打印完参数部分的字符串后,再打印⼀个冒号和⼀个空格,再打印错误信息。

6、perror 函数的使用

void perror ( const char * str );

将 errno 的值解释为错误消息,并将其打印到 stderr(标准错误输出流,通常是控制台),可以选择在它前面加上 str 中指定的自定义消息。

#include <stdio.h>
#include <string.h>
#include <errno.h>
int main ()
{
 FILE * pFile;
 pFile = fopen ("unexist.ent","r");
 if (pFile == NULL)
 perror("Error opening file unexist.ent");
 return 0;
}

输出:

Error opening file unexist.ent: No such file or directory

总结

本篇博客就结束啦,谢谢大家的观看,如果公主少年们有好的建议可以留言喔,谢谢大家啦!

相关推荐
代码雕刻家7 分钟前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
一个闪现必杀技10 分钟前
Python入门--函数
开发语言·python·青少年编程·pycharm
Fan_web14 分钟前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
龙图:会赢的17 分钟前
[C语言]--编译和链接
c语言·开发语言
sp_fyf_202423 分钟前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
XKSYA(小巢校长)2 小时前
NatGo我的世界联机篇
开发语言·php
Cons.W2 小时前
Codeforces Round 975 (Div. 1) C. Tree Pruning
c语言·开发语言·剪枝
我是哈哈hh2 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
憧憬成为原神糕手2 小时前
c++_ 多态
开发语言·c++
VBA63372 小时前
VBA信息获取与处理第三个专题第三节:工作薄在空闲后自动关闭
开发语言