【编织代码之纵横字符与绚丽字符串](中)

简介: 【编织代码之纵横字符与绚丽字符串]

【编织代码之纵横字符与绚丽字符串](上):https://developer.aliyun.com/article/1424767


1.5、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个字符之前找到源C字符串的结束(通过空字符来标志),则目标字符串将用零填充,直到总共复制了num个字符为止。)
  • 拷贝num个字符从源字符串到目标空间。
  • 如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加'\0',直到num个。


#include<stdio.h>
#include<string.h>
int main()
{
  char arr1[20] = "xxxx you";
  char arr2[] = "love";
  strncpy(arr1, arr2, 4);
  printf("%s\n", arr1);
  return 0;
}


运行结果:



1.6、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.(将源字符串的前num个字符(包括一个终止空字符)追加到目标字符串中。)
  • If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.(如果源字符串中的C字符串长度小于num,则只会复制到终止空字符为止的内容。)


#include <stdio.h>
#include <string.h>
int main()
{
    //char * strncat ( char * destination, const char * source, size_t num );
  char str1[20];
  char str2[20];
  strcpy(str1, "To be ");
  strcpy(str2, "or not to be");
  strncat(str1, str2, 6);//将str2长度为6的字符串追加到str1中
  puts(str1);
  return 0;
}


运行结果:



如果源字符串的长度小于num,则拷贝源字符串只会在目标的后边追加一个'\0'



如果源字符串的长度大于num,则拷贝源字符串只会拷贝num数量的字符串,然后再放一个'\0'



1.7、strncmp


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


  • 比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。

#include <stdio.h>
#include <string.h>
int main()
{
    //int strncmp(const char* str1, const char* str2, size_t num);
    char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
    int n;
    puts("Looking for R2 astromech droids...");
    for (n = 0; n < 3; n++)
    {
        if (strncmp(str[n], "R2xx", 2) == 0)//将str长度为2的字符串与R2xx比较
        {
            printf("found %s\n", str[n]);
        }
    }
    return 0;
}


运行结果:



1.8、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中的位置的指针作为返回值返回,如果str2不是str1的一部分,则返回空指针。)


#include <stdio.h>
#include <string.h>
int main()
{
    //​​​​​​​char * strstr ( const char *str1, const char * str2);
  char str[] = "simple string";
  char* pch;
  pch = strstr(str, "simple");//将"simple"第一次出现在str中的位置返回
  printf("%s\n", pch);
  return 0;
}


运行结果:



模拟实现strstr:


char* my_strstr(const char* str1, const char* str2)
{
  char* cp = str1;
  char* s1 = cp;
  char* s2 = str2;
  if (!*str2)
    return str1;
  while (*cp)
  {
    //开始匹配
    s1 = cp;
    s2 = str2;
    while (*s1 && *s2 && *s1 == *s2)
    {
      s1++;
      s2++;
    }
    if (!*s2)
      return cp;
    cp++;
  }
  return NULL;
}


代码图解:



1.9、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* p = "hello@xioayu.diaomao";
  const char* sep = ".@";
  char arr[30];
  char* str = NULL;
  strcpy(arr, p);//将数据拷贝一份,处理arr数组的内容
  for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep))
  {
    printf("%s\n", str);
  }
}


运行结果:



1.10、strerror


char * strerror ( int errnum );


  • 返回错误码,所对应的错误信息。
  • 库函数在执行的时候,发生了错位,会将一个错误码存放errno这个变量当作,errno是C语言提供的一个全局变量


我们来看看一些错误码对应得错误信息


#include <stdio.h>
#include <string.h>
int main()
{
  for (int i = 0; i < 10; i++)
  {
    printf("[%d]错误码对应的错误信息是:%s\n", i, strerror(i));
  }
  return 0;
}


运行结果:



实例代码:


#include <stdio.h>
#include <string.h>
#include <errno.h>//必须包含的头文件
int main()
{
    FILE* pFile;
    pFile = fopen("unexist.txt", "r");
    if (pFile == NULL)
        printf("%s\n", strerror(errno));
        //perror("fopen");也可以输出错误信息
    fclose(pFile);//关闭文件
    return 0;
}


运行结果:



【编织代码之纵横字符与绚丽字符串](下):https://developer.aliyun.com/article/1424786

相关文章
|
2天前
|
Linux 编译器 C语言
Linux应用开发基础知识——字符文字编码(五)
Linux应用开发基础知识——字符文字编码(五)
64 0
Linux应用开发基础知识——字符文字编码(五)
|
2天前
|
C语言
c语言编程练习题:7-3 输出带框文字
本题要求编写程序,输出指定的带框文字。
57 0
|
2天前
|
算法
算法编程(二十八):重新排列单词间的空格
算法编程(二十八):重新排列单词间的空格
36 0
用word快速将数字字体换成新罗马详细简单方法
用word快速将数字字体换成新罗马详细简单方法
3565 0
用word快速将数字字体换成新罗马详细简单方法
|
2天前
|
索引
leetcode代码记录(Z 字形变换
leetcode代码记录(Z 字形变换
11 1
|
2天前
【编织代码之纵横字符与绚丽字符串](上)
【编织代码之纵横字符与绚丽字符串]
|
2天前
【编织代码之纵横字符与绚丽字符串](下)
【编织代码之纵横字符与绚丽字符串]
|
2天前
|
算法 索引
算法编程(二十):仅仅反转字母
算法编程(二十):仅仅反转字母
29 0
|
12月前
|
小程序 索引
如何实现文字逐个出现的打字机效果
今天分享一下如何在微信小游戏制作工具中实现文字逐个出现的打字机效果,这个小功能可以用于游戏中的文字对白的展示,如果你要做的是一个文字类游戏的话,那么肯定用的上。
97 0
【编程】89%的人不知道的字符拼接成字符串的注意点
【编程】89%的人不知道的字符拼接成字符串的注意点
66 0