字符函数和字符串函数(上)——“C”

简介: 字符函数和字符串函数(上)——“C”

各位CSDN的uu们你们好呀,今天小雅兰来给大家介绍一个全新的知识点,就是字符函数和字符串函数啦,其实其中有些函数我之前已经学习过了,比如strlen、strcpy;也有一些之前不是很熟悉的函数,比如strstr、strtok、strerror等等。话不多说啦,现在,让我们进入字符函数和字符串函数的世界吧


求字符串长度


       strlen


长度不受限制的字符串函数


       strcpy


       strcat


       strcmp


长度受限制的字符串函数介绍


       strncpy


       strncat


       strncmp


C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在


常量字符串中或者字符数组中。


字符串常量适用于那些对它不做修改的字符串函数.


strlen

4c189fb31a4e42f3b1137a95f90f9b8a.png

618a431617b6483997a437c474969c6a.png

size_t strlen ( const char * str );

字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
  char arr[] = "abc\0def";
  int len = strlen(arr);
  printf("%d\n", len);
  return 0;
}

b7b094f0dd3a45439c6131ee1a042cc8.png

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
  char arr[] = "abcdef";
  int len = strlen(arr);
  printf("%d\n", len);
  return 0;
}

2e9ec91a2396472fa3275d3227a91faa.png

abcdef后面隐藏了一个\0

参数指向的字符串必须要以 '\0' 结束。

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
  char arr[3] = { 'a','b','c'};
  int len = strlen(arr);
  printf("%d\n", len);
  return 0;
}

21a07c6875824ca6aeb5c7edd25d93b8.png如果字符串不以\0结束,那么,结果就是一个随机值

注意函数的返回值为size_t,是无符号的

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
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;
}

4bf00416c8524bc8823497a31a82697a.png

模拟实现strlen


三种方式:


       1.计数器的方式


       2.递归的方式


       3.指针-指针的方式


函数递归+青蛙跳台阶——“C”_认真学习的小雅兰.的博客-CSDN博客


指针——“C”_认真学习的小雅兰.的博客-CSDN博客


1.计数器的方式

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
int my_strlen(const char* str)
{
  //计数器的方式
  int count = 0;
  assert(str != NULL);
  while (*str != '\0')
  {
    str++;
    count++;
  }
  return count;
}
int main()
{
  char arr[] = "abcdef";
  int len = my_strlen(arr);
  printf("%d\n", len);
  return 0;
}

66fb5841cb274ad299e78a39d2549f4f.png

2.递归的方式

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
//不能创建临时变量,求字符串的长度
int my_strlen(const char * str)
{
  if(*str=='\0')
    return 0;
  else
    return 1 + my_strlen(str+1);
}
int main()
{
  char arr[] = "abcdef";
  int len = my_strlen(arr);
  printf("%d\n", len);
  return 0;
}

d3729a2e8f84479f8e624ec5572b29a5.png

3.指针-指针的方式

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
//指针-指针的方式
int my_strlen(char * s)
{
  char * p = s;
  while( * p != '\0')
       p++;
  return p-s;
}
int main()
{
  char arr[] = "abcdef";
  int len = my_strlen(arr);
  printf("%d\n", len);
  return 0;
}

7fcfbd6cb81a42ce988c64a536252e12.png

strcpy

ee9d1f70202f4f9e89836fe2fe6d6944.png

c288a072a6f24e78b962b51a0c2517c3.png

char* strcpy(char * destination, const char * source );  


Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).


源字符串必须以 '\0' 结束。

#include<stdio.h>
#include<string.h>
int main()
{
  char arr1[3] = { 'a','b','c' };
  char arr2[20] = "xxxxxx";
  strcpy(arr2, arr1);
  printf("%s\n", arr2);
  return 0;
}

这样程序直接崩溃了

563c1ff25a4f499c83476e5f5b3bed78.png

会将源字符串中的 '\0' 拷贝到目标空间。

目标空间必须足够大,以确保能存放源字符串。

#include<stdio.h>
#include<string.h>
int main()
{
    //错误的示范
  char arr1[20] = "abcdefghi";
  char arr2[3] = "";
  strcpy(arr2, arr1);
  printf("%s\n", arr2);
  return 0;
}

ec8d8bfdda804e5ba85084efab30a001.png

目标空间必须可变。

#include<stdio.h>
#include<string.h>
int main()
{
  //错误的示范
  char* p = "abcdefghi";
  char arr2[20] = "hehe";
  strcpy(p, arr2);
  printf("%s\n", p);
  return 0;
}

532a31dac6e145639935c993e3b95092.png

模拟实现strcpy

#include<stdio.h>
#include<string.h>
//1.参数顺序
//2.函数的功能,停止条件
//3.assert
//4.const修饰指针
//5.函数返回值
//6.题目出自《高质量C/C++编程》书籍最后的试题部分
//返回的是目标空间的起始地址
#include<assert.h>
char * my_strcpy(char * dest, const char* src)
{
  char * ret = dest;
  assert(dest!=NULL);
  assert(src != NULL);
  while ((*dest++ = *src++))
  {
    ;
  }
  return ret;
}
int main()
{
  char arr1[] = "hehe";
  char arr2[20] = { 0 };
  my_strcpy(arr2, arr1);
  printf("%s\n", arr2);
  return 0;
}

8c6803a8c474495abf4c21177c252ef5.pngstrcat


bc55cd6124114c0da2bf77aec3a5ccb7.png

4d77c8087ad64076b35723eb72fe22b7.png

char * strcat ( char * destination, const char * source );  


#include<stdio.h>
#include<string.h>
int main()
{
  char arr1[20] = "hello ";
  char arr2[] = "world";
  //追加
  strcat(arr1, arr2);
  printf("%s\n", arr1);
  return 0;
}

065ee65caac645e2bad6b48b5ab0af68.png

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.


源字符串必须以 '\0' 结束。

#include<stdio.h>
#include<string.h>
int main()
{
  char arr1[20] = "hello \0xxxxxxxxxxxx";
  char arr2[] = "world";
  //追加
  strcat(arr1, arr2);
  printf("%s\n", arr1);
  return 0;
}

1bff2e69f10b44f99d59b1e98d42e6de.png

目标空间必须有足够的大,能容纳下源字符串的内容。

目标空间必须可修改。

模拟实现strcat

5fab4303135b4c92836d4cb6853590e6.png

#include<stdio.h>
#include<assert.h>
char* my_strcat(char* dest, const char* src)
{
  char* ret = dest;
  assert(dest != NULL);
  assert(src != NULL);
  //找目标空间的\0
  while (*dest!='\0')
  {
    dest++;
  }
  //拷贝
  while ((*dest++ = *src++))
  {
    ;
  }
  return ret;
}
int main()
{
  char arr1[20] = "hello ";
  char arr2[] = "world";
  //追加
  my_strcat(arr1, arr2);
  printf("%s\n", arr1);
  return 0;
}

f08131a6ad404e88a500173a5c6e8463.png

绝对不能自己给自己追加!!!

#include<stdio.h>
#include<assert.h>
char* my_strcat(char* dest, const char* src)
{
  char* ret = dest;
  assert(dest != NULL);
  assert(src != NULL);
  //找目标空间的\0
  while (*dest != '\0')
  {
    dest++;
  }
  //拷贝
  while ((*dest++ = *src++))
  {
    ;
  }
  return ret;
}
int main()
{
  char arr1[20] = "bit";
  //追加
  my_strcat(arr1, arr1);
  printf("%s\n", arr1);
  return 0;
}

e0859423a9fb459f8efd8e29d7e42e6f.png

strcmp

8ae2b80340ca4eb3b5d6d69d2cafd87d.png

1804095583414ca6b0c6d9ad196143a7.png

“abcdef"=="bbcdef",这里比较的是两个字符串首字符的地址,而并不是字符串的内容


比较两个字符串内容的时候,不能使用==,应该使用strcmp


               int strcmp ( const char * str1, const char * str2 );


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.


标准规定:


第一个字符串大于第二个字符串,则返回大于0的数字

第一个字符串等于第二个字符串,则返回0

第一个字符串小于第二个字符串,则返回小于0的数字

#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
  char arr1[] = "abcdef";
  char arr2[] = "bbcdef";
  int ret = strcmp(arr1, arr2);
  printf("%d\n", ret);
  return 0;
}

d759eb49c39042619b68e9a24d0379b9.png模拟实现strcmp

#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
  assert(str1 != NULL);
  assert(str2 != NULL);
  while (*str1 == *str2)
  {
    if (*str1 == '\0')
    {
      return 0;
    }
    str1++;
    str2++;
  }
  if (*str1 > *str2)
  {
    return 1;
  }
  else
  {
    return -1;
  }
}
int main()
{
  char arr1[] = "abcdef";
  char arr2[] = "bbcdef";
  int ret = my_strcmp(arr1, arr2);
  printf("%d\n", ret);
  return 0;
}

另一种写法:

#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
  assert(str1 != NULL);
  assert(str2 != NULL);
  while (*str1 == *str2)
  {
    if (*str1 == '\0')
    {
      return 0;
    }
    str1++;
    str2++;
  }
  return *str1 - *str2;
}
int main()
{
  char arr1[] = "abcdef";
  char arr2[] = "bbcdef";
  int ret = my_strcmp(arr1, arr2);
  printf("%d\n", ret);
  return 0;
}

b2ce651c2730482180d77e0b394cf48a.png

strncpy

0275aac7dd3b4732827ca0e926ec615d.png

cef58a8df6eb4c6eb81521bd8fbf3611.png

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,则拷贝完源字符串之后,在目标的后边追加0,直到num个。

#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
  char arr1[] = "abcdef";
  char arr2[5] = { 0 };
  strncpy(arr2, arr1, 3);
  printf("%s\n", arr2);
  return 0;
}

7dfbd3325af640db94a6b8918b76ee8f.png

 strncat

aec8bc77666d4281901d65ef82ea2374.png

8936fc33e3f147c0adfcfac7eb51c78b.png

char * strncat ( char * destination, const char * source, size_t num );  


Appends the first num characters of source to destination, plus a terminating null-character.

If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.

#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
  char arr1[20] = "hello \0xxxxxxxx";
  char arr2[] = "world";
  strncat(arr1, arr2, 3);
  printf("%s\n", arr1);
  return 0;
}

882d1efa8f344fe39fe11e7a42c6496e.png

strncmp

80603b72932d4f80b0006590cb74373d.png

1098ef29b42648abb9c62073079303e4.png

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

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

#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
  char arr1[] = "abcdef";
  char arr2[] = "abcq";
  int ret = strncmp(arr1, arr2, 4);
  printf("%d\n", ret);
  return 0;
}

4d0ed33baa094d32855ef30369ae3dfd.png

好啦,小雅兰今天的内容就到这里啦,还要继续加油呀!!!

621f74cc831c408fbc4e162dd63215c8.jpg

相关文章
|
19天前
字符串函数
字符串函数
|
19天前
|
C语言
字符函数和字符串函数
字符函数和字符串函数
54 0
|
10月前
|
存储 C语言
字符串函数介绍&应用(二)
字符串函数介绍&应用
|
6月前
|
C语言
字符函数和字符串函数(上)
字符函数和字符串函数
|
6月前
|
C语言
字符函数和字符串函数(下)
字符函数和字符串函数(下)
|
6月前
|
C语言
详解字符函数和字符串函数-2
详解字符函数和字符串函数
28 0
|
6月前
详解字符函数和字符串函数-1
详解字符函数和字符串函数
25 0
|
7月前
|
编译器
C详细的字符串函数
C详细的字符串函数
35 0
|
8月前
|
编译器 C语言 Python
字符函数和字符串函数(上)
字符函数和字符串函数(上)
30 0
|
8月前
|
C语言 Python
字符函数和字符串函数(下)
字符函数和字符串函数(下)
31 0
字符函数和字符串函数(下)