小工具知识网

当前位置: 首页 > 行业知识 > [C语言]——字符函数和字符串函数

行业知识

[C语言]——字符函数和字符串函数

2026-10-30 15:30:43 来源:互联网转载

���录

一.字符分类函数

二.字符转换函数

三.strlen的使用和模拟实现(string length)

四.strcpy的使用和模拟实现(string copy字符串拷贝)

五.strcat的使用和模拟实现(追加一个字符串)

六.strcmp的使用和模拟实现(string compare)

七.strncpy函数的使用

 八.strncat函数的使用(自己给自己追加更合适)

 九.strncmp函数的使用

十.strstr的使用和模拟实现(在一个字符串中找另一个字符串)

十一. strtok函数的使用

十二.strerror 函数的使用


一.字符分类函数

    C语言中有⼀系列的函数是专⻔做字符分类的,也就是⼀个字符是属于什么类型的字符的。 这些函数的使用都需要包含⼀个头文件是 ctype.h 。

 这些函数的使用方法非常类似,我们就讲解⼀个函数的事情,其他的非常类似:

int islower ( int c );
练习: 写⼀个代码,将字符串中的⼩写字⺟转⼤写,其他字符不变。
#include #include int main (){ int i = 0; char str[] = "Test String.\n"; char c; while (str[i]) {   c = str[i];   if (islower(c))     c -= 32;    putchar(c);    i++; } return 0;}

二.字符转换函数

C语言提供了2个字符转换函数:

int tolower ( int c ); //将参数传进去的⼤写字⺟转⼩写 int toupper ( int c ); //将参数传进去的⼩写字⺟转⼤写

上面的代码,我们将小写转大写,是-32完成的效果,有了转换函数,就可以直接使用 tolower 函数。

#include #include int main (){ int i = 0; char str[] = "Test String.\n"; char c; while (str[i]) {  c = str[i];  if (islower(c))     c = toupper(c);   putchar(c);   i++; } return 0;}

三.strlen的使用和模拟实现(string length)

size_t strlen ( const char * str );
  • 字符串以 '
  • strlen函数返回的是在字符串中 '
  • 参数指向的字符串必须要以 '
  • 注意函数的返回值为size_t,是无符号的( 易错 )
  • ' 结束。 ' 前面出现的字符个数(不包含 '
  • strlen的使用需要包含头文件
  • ' )。 ' 作为结束标志
    #include #include int main(){ const char* str1 = "abcdef"; const char* str2 = "bbb"; if(strlen(str2)-strlen(str1)>0) {  printf("str2>str1\n"); }  else {  printf("srt1>str2\n"); } return 0;}
    //计数器⽅式int my_strlen(const char * str){ int count = 0; assert(str); while(*str) {  count++;  str++; } return count;}
    //不能创建临时变量计数器(count)int my_strlen(const char * str){ assert(str); if(*str == '
    //指针-指针的⽅式int my_strlen(char *s){ assert(str); char *p = s; while(*p != ‘

    四.strcpy的使用和模拟实现(string copy字符串拷贝)

    ’ ) p++; return p-s;}
    ') return 0; else return 1+my_strlen(str+1);}
    char* strcpy(char * destination, const char * source );
  • 学会strlen函数的模拟实现
  • Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
  • strlen的模拟实现: 方式1:
  • 源字符串必须以 '
  • 会将源字符串中的 '
  • 目标空间必须足够大,以确保能存放源字符串。
  • ' 拷贝到目标空间。 ' 结束。

    方式2:

  • 目标空间必须可修改。
  • 方式3:

    //1.参数顺序//2.函数的功能,停⽌条件//3.assert//4.const修饰指针//5.函数返回值//6.题⽬出⾃《⾼质量C/C++编程》书籍最后的试题部分 char *my_strcpy(char *dest, const char*src) {    char *ret = dest;   assert(dest != NULL);   assert(src != NULL);   while((*dest++ = *src++))   {     ;   }   return ret; }

    五.strcat的使用和模拟实现(追加一个字符串)

  • 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.
    • 源字符串必须以 '
    • ⽬标字符串中也得有
    • ⽬标空间必须有⾜够的⼤,能容纳下源字符串的内容。
    • ,否则没办法知道追加从哪⾥开始。 ' 结束。
    • ⽬标空间必须可修改。
    • char *my_strcat(char *dest, const char*src) {   char *ret = dest;   assert(dest != NULL);   assert(src != NULL);   while(*dest)//找到目标空间的

      六.strcmp的使用和模拟实现(string compare)

      { dest++; } //拷贝 自己给自己追加——***循环 while((*dest++ = *src++)) { ; } return ret;}
    • 学会模拟实现。 strcpy的模拟实现:
    • 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.
    • int my_strcmp (const char * str1, const char * str2){ int ret = 0 ; assert(src != NULL); assert(dest != NULL); while(*str1 == *str2) {  if(*str1 == '

      七.strncpy函数的使用

      ') return 0;//均为
      char * strncpy ( char * destination, const char * source, size_t num );
      ,结束 str1++; str2++; } return *str1-*str2;}
      • 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.
      •  八.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指向的字符串末尾,再追加⼀个
        /* strncat example */#include #include int main (){ char str1[20]; char str2[20]; strcpy (str1,"To be "); strcpy (str2,"or not to be"); strncat (str1, str2, 6); printf("%s\n", str1); return 0;}
        字符)。
      •  九.strncmp函数的使用

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

        十.strstr的使用和模拟实现(在一个字符串中找另一个字符串)

      • 字符串⾃⼰给⾃⼰追加,如何? 模拟实现strcat函数:
        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中第⼀次出现的位置)。
        • /* strstr example */#include #include int main (){ char str[] ="This is a simple string"; char * pch; pch = strstr (str,"simple"); strncpy (pch,"sample",6); printf("%s\n", str); return 0;}
        • 标准规定: ◦ 第⼀个字符串⼤于第⼆个字符串,则返回⼤于0的数字 ◦ 第⼀个字符串等于第⼆个字符串,则返回0 ◦ 第⼀个字符串⼩于第⼆个字符串,则返回⼩于0的数字 ◦ 那么如何判断两个字符串? ⽐较两个字符串中 对应位置上字符ASCII码值 的⼤⼩。

          比较的是对应位置的字符大小,小的字符所在的字符串小于另外一个字符串,比较的不是字符串长度

          strcmp函数的模拟实现:
          char * strstr (const char * str1, const char * str2){       char *cp = (char *) str1;       char *s1, *s2;       if ( !*str2 )          return((char *)str1);       while (*cp)       {          s1 = cp;          s2 = (char *) str2;          while ( *s1 && *s2 && !(*s1-*s2) )//*s1-*s2即为*s1=*s2    //例如str1:abcdef

          十一. strtok函数的使用

          //str2:abcdef
        • sep参数指向⼀个字符串,定义了用作分隔符的字符集合
        • //1.s1找到了
        • 第⼀个参数指定⼀个字符串,它包含了0个或者多个由sep字符串中⼀个或者多个分隔符分割的标记。sep:'@'  '.'
        • ,*s2=='
        • strtok函数找到str中的下⼀个标记,并将其用 
        • strtok函数的第⼀个参数不为NULL ,函数将找到str中第⼀个标记,strtok函数将保存它在字符串中的位置。
        • 结尾,返回⼀个指向这个标记的指针。(注: strtok函数会改变被操作的字符串,所以在使⽤strtok函数切分的字符串⼀般都是临时拷贝的内容并且可修改。) zhangsan@163.com  zhangsan
        • strtok函数的第⼀个参数为 NULL , 函数将在同⼀个字符串中被保存的位置开始,查找下⼀个标记。
        • @163.com(指向z的前面)
        • ',找到了 //2.s2找到了
          #include  #include  int main() {     char arr[] = "192.168.6.111";     char* sep = ".";     char* str = NULL;     for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep))     {//for循环初始化部分只执行一次        printf("%s\n", str);     }     return 0; }
          ,找到了 //3.*s1!=*s2 s1++, s2++; if (!*s2) return(cp); cp++; } return(NULL);}

          十二.strerror 函数的使用

          char * strerror ( int errnum );
            #include #include #include  //我们打印⼀下0~10这些错误码对应的信息int main(){  int i = 0;  for (i = 0; i 
          • 拷贝num个字符从源字符串到目标空间。如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。如果目标空间小于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.(如果source 指向的字符串的长度小于num的时候,只会将字符串中到\0 的内容追加到destination指向的字符串末尾)。 ⽐较str1和str2的前num个字符,如果相等就继续往后比较,最多⽐较num个字母,如果提前发现不⼀样,就提前结束,⼤的字符所在的字符串⼤于另外⼀个。 如果num个字符都相等,就是相等返回0.
              • The matching process does not include the terminating null-characters, but it stops there.(字符串的⽐较匹配不包含 \0 字符,以 \0 作为结束标志)。
                •  如果字符串中不存在更多的标记,则返回 NULL 指针。 strerror函数可以把 参数部分错误码对应的错误信息的字符串地址返回来 。 在不同的系统和C语⾔标准库的实现中都规定了⼀些错误码,⼀般是放在 errno.h 这个头⽂件中说明的,C语言程序启动的时候就会使用⼀个全面的变量errno来记录程序的当前错误码,只不过程序启动的时候errno是0,表示没有错误,当我们在使⽤标准库中的函数的时候发生了某种错误,就会将对应的错误码,存放在errno中,而⼀个错误码的数字是整数很难理解是什么意思,所以每⼀个错误码都是有对应的错误信息的。strerror函数就可以将错误对应的错误信息字符串的地址返回。

上一篇:学校买的医疗保险怎么备案

下一篇:两联供主机怎么接