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

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



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

总结

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

相关文章
|
2月前
|
C语言
C语言中10种常见的字符串函数你都掌握了吗?
C语言中10种常见的字符串函数你都掌握了吗?
|
2月前
|
C语言
C语言-----字符串函数(1)
C语言-----字符串函数(1)
11 0
|
2月前
|
C语言
C语言:字符函数和字符串函数(一篇拿捏字符串函数!)
C语言:字符函数和字符串函数(一篇拿捏字符串函数!)
44 0
|
2天前
|
存储 编译器 C语言
C语言:字符函数 & 字符串函数 & 内存函数
C语言:字符函数 & 字符串函数 & 内存函数
10 2
|
16天前
|
C语言
C语言:字符函数和字符串函数(strlen strcat strcmp strncmp等函数和模拟实现)
C语言:字符函数和字符串函数(strlen strcat strcmp strncmp等函数和模拟实现)
|
1月前
|
程序员 C语言 开发者
C语言库函数 — 字符串函数(含模拟实现字符串函数)
C语言库函数 — 字符串函数(含模拟实现字符串函数)
37 0
|
1月前
|
算法 C语言
字符串函数-C语言
字符串函数-C语言
|
1月前
|
C语言
【C语言】字符串函数strcpy&&strcat&&strcmp&&strstr的使⽤和模拟实现2
【C语言】字符串函数strcpy&&strcat&&strcmp&&strstr的使⽤和模拟实现
|
1月前
|
存储 C语言
【C语言】字符串函数strcpy&&strcat&&strcmp&&strstr的使⽤和模拟实现1
【C语言】字符串函数strcpy&&strcat&&strcmp&&strstr的使⽤和模拟实现
|
2月前
|
C语言
C语言第二十五弹---字符函数和字符串函数(上)
C语言第二十五弹---字符函数和字符串函数(上)