常见的字符函数与字符串函数介绍

前言

C语言中对字符与字符串的处理很是频繁,但是C语言中并没有字符串类型的变量,字符串通常存放在常量字符串或者字符数组中。字符串常量适用于那些对它不做任何修改的字符串函数。

函数功能简介与再实现

1、 strlen函数

函数原型:size_t strlen (const char * str);
该函数用于计算字符串的长度

函数简介

  1. 字符串一定以'\0'作为结束标志,strlen函数返回的是字符串'\0'之前出现的字符个数(不包括'\0'
  2. 参数指向的字符串必须要以'\0'结束
  3. 注意函数的返回值为size_t,是无符号的(易错
  4. 学会strlen函数的模拟实现

模拟实现

计数器方式实现函数

int my_strlen (const char * str)
{int count = 0;while (*str){count++;str++;}return count;
}

不创建临时变量计数,使用递归方式

int my_strlen (const * str)
{if(*str == '\0')return 0;elseretrun 1 + my_strlen(*str+1);
}

指针-指针的方式实现

int my_strlen (char * s)
{char * p = s;while (*p != '\0')p++;return p-s;
}

三种方式均可以实现strlen函数,都应进行掌握。

2、strcpy函数

函数原型 char * strcpy(char * destination, const char * source);
该函数用于实现字符串的拷贝

函数简介

  1. Copies the C string pointed by source into the array pointed by
    destination, including the terminating null character (and stopping
    at that point).
  2. 源字符串必须以 ‘\0’ 结束。
  3. 会将源字符串中的 ‘\0’ 拷贝到目标空间。
  4. 目标空间必须足够大,以确保能存放源字符串。
  5. 目标空间必须可变。
  6. 学会模拟实现

模拟实现

//1.参数顺序
//2.函数功能与停止条件
//3.assert
//4.const修饰指针
//5.函数的返回值
char* my_strcpy(char* dest, const char* src)
{char* ret = dest;assert(dest != NULL);assert(src != NULL);while ((*dest++ = *src++)){;}return ret;

3、strcat函数

函数原型`char * strcat(char * destination,const char * source);
该函数用于实现字符串的连接

函数简介

  1. Appends a copy of the source string to the destination string. The
    terminating null character indestination 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.
  2. 源字符串必须以 '\0' 结束。
  3. 目标空间必须有足够的大,能容纳下源字符串的内容。
  4. 目标空间必须可修改。
  5. 字符串自己不能给自己追加

模拟实现

char* my_strcat(char* dest, const char* src)
{char* ret = dest;while (*dest){dest++;}while (*dest++ = *src++){;}return ret;
}

4、strcmp函数

函数原型:int strcmp (const char * str1,const char * str2 );
该函数用于比较两个字符串的大小

函数简介

  1. 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.
  2. 标准规定: 第一个字符串大于第二个字符串,则返回大于0的数字
    第一个字符串等于第二个字符串,则返回0
    第一个字符串小于第二个字符串,则返回小于0的数字

模拟实现

int my_strcmp(const char* src, const char* dst)
{int ret = 0;assert(src != NULL);assert(dest != NULL);while (!(ret = *(unsigned char*)src - *(unsigned char*)dst) && *dst)++src, ++dst;if (ret < 0)ret = -1;else if (ret > 0)ret = 1;return(ret);
}

5、strncpy函数

函数原型:char * strncpy (char * destination,const char * source, size_t num);
该函数用于从源字符中拷贝num个字符到目标空间。

函数简介·

  1. 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.
  2. 拷贝num个字符从源字符串到目标空间
  3. 如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个
  4. 如果目标空间大小大于num,则目标空间num之后的内容不会发生改变

模拟实现

#include<stdio.h>
#include<stdlib.h>char* My_strncpy(const char*a, char* b, size_t sz)
{size_t i = 0;for(i = 0; i < sz; i++)//拷贝n个字符{*(b+i) = *(a+i);}*(b+i) = '\0';return b;
}int main()
{char a[] = "asdfdgddh";char b[] = {0};char *p = My_strncpy(a, b, 3*sizeof(a[0]));printf("%s", p);system("pause");return 0;
}

6、strncat函数

函数原型:char * strncat(char * destination ,const char * source, size_t num);
该函数用于再在目标空间的原有内容之后追加num个字符

函数简介

  1. Appends the first num characters of source to destination, plus a terminating null-character.
  2. If the length of the C string in source is less than num, only the content up to the terminating nullcharacter is copied.

strncat example

#include <stdio.h>
#include <string.h>
int main ()
{char str1[20];char str2[20];strcpy (str1,"To be ");strcpy (str2,"or not to be");strncat (str1, str2, 6);puts (str1);return 0;
}

运行结果

To be or not

模拟实现

#include<stdlib.h>
#include<stdio.h>
#include<assert.h>char *My_strncat(char *n_a, const char *n_b, size_t sz)
{assert(n_a);assert(n_b);char *c = n_a;while(*n_a != '\0')//找到‘\0’的位置{n_a++;}while(sz--)//向后追加字符{*n_a++ = *n_b++;}*n_a = '\0';//最后结束时加'\0'return c;
}int main()
{char a[] = "asdfghjk";char b[] = "ZXCFGHJ";char* p = My_strncat(a, b, 3*sizeof(a[0]));printf("%s", p);system("pause");return 0;
}

7、strncmp函数

函数原型:int strncmp (const char * str1, sonst char *str2, size_t num);
该函数用于比较两个字符串前num个字符的大小

函数简介

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

举个栗子

#include <stdio.h>
#include <string.h>
int main ()
{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){printf ("found %s\n",str[n]);}return 0;
}

运行结果

Looking for R2 astromech droids...
found R2D2found R2A6

8、strstr函数

函数原型:char * strstr (const char * ,const char * );
该函数用于在arr1字符串中查找arr2字串第一次出现的位置,并返回其首地址

函数简介:

  1. Returns a pointer to the first occurrence of str2 in str1, or a null
    pointer if str2 is not part of str1.

举个栗子

#include <stdio.h>
#include <string.h>
int main ()
{char str[] ="This is a simple string";char * pch;pch = strstr (str,"simple");strncpy (pch,"sample",6);puts (pch);return 0;
}

运行结果

sample string

模拟实现

char* my_strstr(const char* str1, const char* str2)
{assert(str1);assert(str2);char* cp = (char*)str1;char* substr = (char*)str2;char* s1 = NULL;if (*str2 == '\0')return NULL;while (*cp){s1 = cp;substr = str2;while (*s1 && *substr && (*s1 == *substr)){s1++;substr++;}if (*substr == '\0')return cp;cp++;}
}

9、strtok函数

函数原型:char * strtok (char * str ,const char * sep);

函数简介

  1. sep参数是个字符串,定义了用作分隔符的字符集合

  2. 第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。

  3. strtok函数找到str中的下一个标记,并将其用 \0结尾,返回一个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)

  4. strtok函数的第一个参数不为 NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。

  5. strtok函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记。

  6. 如果字符串中不存在更多的标记,则返回 NULL 指针。

具体解释

char * arr[] = "lpt@bitedu.tech";

其中,@.称为分隔符

char * p = "@,.";

p为分隔符的合集

strtok(arr,p);

此函数第一次调用,会将@改为\0,并返回l的地址
第二次调用,会将.改为\0,并返回b的地址

举个栗子

#include <stdio.h>
#include <string.h>
int main ()
{char str[] ="- This, a sample string.";char * pch;printf ("Splitting string \"%s\" into tokens:\n",str);pch = strtok (str," ,.-");while (pch != NULL){printf ("%s\n",pch);pch = strtok (NULL, " ,.-");}return 0;
}

运行结果

Splitting string "- This, a sample string." into tokens:
This
a
sample
string

10、strerror函数

函数原型:char * strerror (int errum);
该函数为错误报告函数,会将错误码转换为对应的错误信息

函数简介

  1. 必须包含的头文件#include <errno.h>;
错误码 错误信息
0 Not error
1 Operation not permitted
2 No much file or directory
3 No much process

举个栗子

#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)); //errno: Last error number return 0;
}
Edit & Run

11、字符分类函数

函数 如果它的参数符合下列条件就返回为真
iscntrl 任何控制字符
isspace 空白字符:空格‘ ’,换页‘\f’,换行’\n’,回车‘\r’,制表符’\t’或者垂直制表符’\v’
isdigit 十进制数字 0~9
isxdigit 十六进制数字,包括所有十进制数字,小写字母a至f,大写字母A~F
islower 小写字母a~z
isupper 大写字母A~Z
isalpha 字母a至z或A~Z
isalnum 字母或者数字,a~z, A~Z, 0~9
ispunct 标点符号,任何不属于数字或者字母的图形字符(可打印)
isgraph 任何图形字符
isprint 任何可打印字符,包括图形字符和空白字符

12、字符转换函数

int tolower (int c);
int toupper (int c);

举例

#include <stdio.h>
#include <string.h>
int main()
{int i = 0;char str[] = "Test String.\n";char c;while (str[i]){c = str[i];if (isupper(c))c = tolower(c);putchar(c);i++;}return 0;
}

运行结果

test string.

未完待续

常见的字符函数与字符串函数介绍(1)相关推荐

  1. 字符函数和字符串函数详解(二)strncpy strncat strncmp strstr strtok(及其模拟实现)

     系列文章目录 字符函数和字符串函数详解(一)strlen strcpy strcat strcmp 字符函数和字符串函数详解(二)strncpy strncat strncmp strstr str ...

  2. 字符函数和字符串函数(上)

    字符函数和字符串函数 前言 一. strlen 1. '\0' 2. 返回值 3. strlen的模拟实现 二. strcpy 1. 源字符串必须以'\0'结束 2. 会将源字符中的'\0'拷贝到目标 ...

  3. c语言中常用的字符函数以及字符串函数

    文章目录 前言 一.常用字符串函数 1.strlen() 2.strcpy() 3.strcat() 4.strcmp() 5.strstr() 6.memcpy() 6.memmove() 二.qs ...

  4. 字符函数和字符串函数_R中的字符串–函数及其操作

    字符函数和字符串函数 Strings are generally a one-dimensional (1D) arrays that contain single or multiple value ...

  5. 用C语言模拟实现字符函数与字符串函数

    用C语言模拟实现字符函数与字符串函数 strncat.strncpy.atoi 若使用本文相关代码,还请动手点个赞!!! #define _CRT_SECURE_NO_WARNINGS 1 #incl ...

  6. 如何用C语言实现各种字符函数和字符串函数strstr、memcpy、memmove、strlen、strcpy、strcmp、strcat

    用C语言模拟实现字符函数与字符串函数 strstr.memcpy.memmove.strlen.strcpy.strcmp.strcat 若使用本文相关代码,还请动手点个赞!!! #define _C ...

  7. mysql strcmp s1 s2_MySQL函数基础——字符串函数详解

    昨天,咱们对MySQL的数学函数进行了讲解,今天,咱们再来解析MySQL字符串函数. 字符串函数主要用来处理数据库中的字符串数据,MySQL中字符串函数有:计算字符串长度函数.字符串合并函数.字符串替 ...

  8. mysql函数编写格式_MySQL函数基础——字符串函数详解

    昨天,咱们对MySQL的数学函数进行了讲解,今天,咱们再来解析MySQL字符串函数. 字符串函数主要用来处理数据库中的字符串数据,MySQL中字符串函数有:计算字符串长度函数.字符串合并函数.字符串替 ...

  9. oracle函数大全指数运算,Oracle 基础语句 函数大全(字符串函数,数学函数,日期函数,逻辑运算函数......

    ORACLE PL/SQL 字符串函数.数学函数.日期函数 --[字符串函数] --字符串截取substr(字段名,起始点,个数) select Name,substr(Name,2,4),subst ...

最新文章

  1. 用JavaScript玩转游戏物理(一)运动学模拟与粒子系统
  2. 马化腾看上了TA:读懂互联网医疗的进化与颠覆
  3. ssh 端口_【科普】SSH都不懂,还搞什么网络
  4. 2013年微软编程之美大赛初赛第二题(博客园居然可以插入代码!!)
  5. r语言 转录本结构及丰度_生信人的R语言视频教程语法篇第三章:数行天下(4)R中的数据——从结构角度划分(数组)...
  6. javaweb开发后端常用技术_Java web开发需要学习哪些技术?
  7. 学计算机笔画,学汉字学笔顺电脑版
  8. 步进电机驱动的使用及程序
  9. python爬取电子病历_利用 BERT 模型解析电子病历
  10. [高项]定性风险分析VS定量风险分析
  11. 用python计算工资工资_薪资计算-Python,薪水,python
  12. 利用jsPDF把图片转成pdf格式保存本地指定目录
  13. visual studio使用时光标变粗
  14. 1041:奇偶数判断
  15. 新能源汽车——EMC
  16. 第三章 动态路由协议
  17. 微信公众号关于百度地图和腾讯地图本地定位api的调用(js)
  18. Python中字符串的驻留机制和常用方法
  19. 易维php使用介绍,Web易维版:AR配置三层组网管理「AP + 傻瓜交换机+三层交换机 + AR」...
  20. 初识whistle代理工具

热门文章

  1. 读书笔记007:《伤寒论》- 手少阴心经
  2. Python+Selenium基础篇之2-打开和关闭火狐浏览器
  3. 【NOIP模拟】健美猫
  4. pat 甲级 1034. Head of a Gang (30)
  5. 20165333 我期望的师生关系
  6. 1.6.2 多表插入
  7. 使用indexOf()算出长字符串中包含多少个短字符串
  8. JavaScript MSN 弹出消息框
  9. 2.2_ 4_ FCFS、SJF、 HRRN调度算法
  10. 【剑指 offer】面试题13:机器人的运动范围(Java)