相关文章推荐
活泼的打火机  ·  ubuntu搭建sftp服务 - 小伍子 ...·  1 年前    · 
豪气的小摩托  ·  【数据库原理及应用】创建与使用触发器-阿里云 ...·  2 年前    · 
老实的领结  ·  docker alpine镜像中遇到 ...·  2 年前    · 
奔跑的猴子  ·  盘点一份JS逆向代码转换为Python代码的 ...·  2 年前    · 
高大的椰子  ·  人流量统计抓帧的时候,frame为空·  2 年前    · 
Code  ›  C语言字符串操作总结大全(超详细)开发者社区
c语言 char函数 字符串函数 char
https://cloud.tencent.com/developer/article/1546686
俊秀的熊猫
2 年前
作者头像
用户6754675
0 篇文章

C语言字符串操作总结大全(超详细)

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 嵌入式单片机 > C语言字符串操作总结大全(超详细)

C语言字符串操作总结大全(超详细)

作者头像
用户6754675
修改 于 2019-12-02 17:41:41
1.4K 0
修改 于 2019-12-02 17:41:41
举报

本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下

1)字符串操作

strcpy(p, p1) 复制字符串

strncpy(p, p1, n) 复制指定长度字符串

strcat(p, p1) 附加字符串

strncat(p, p1, n) 附加指定长度字符串

strlen(p) 取字符串长度

strcmp(p, p1) 比较字符串

strcasecmp忽略大小写比较字符串

strncmp(p, p1, n) 比较指定长度字符串 strchr(p, c) 在字符串中查找指定字符 strrchr(p, c) 在字符串中反向查找 strstr(p, p1) 查找字符串 strpbrk(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找该集合的任一元素 strspn(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找不属于该集合的任一元素的偏移 strcspn(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找属于该集合的任一元素的偏移

* 具有指定长度的字符串处理函数在已处理的字符串之后填补零结尾符

2)字符串到数值类型的转换

strtod(p, ppend) 从字符串 p 中转换 double 类型数值,并将后续的字符串指针存储到 ppend 指向的 char* 类型存储。

strtol(p, ppend, base) 从字符串 p 中转换 long 类型整型数值,base 显式设置转换的整型进制,设置为 0 以根据特定格式判断所用进制,0x, 0X 前缀以解释为十六进制格式整型,0 前缀以解释为八进制格式整型

atoi(p) 字符串转换到 int 整型

atof(p) 字符串转换到 double 符点数

atol(p) 字符串转换到 long 整型

3)字符检查

isalpha() 检查是否为字母字符

isupper() 检查是否为大写字母字符

islower() 检查是否为小写字母字符

isdigit() 检查是否为数字

isxdigit() 检查是否为十六进制数字表示的有效字符 isspace() 检查是否为空格类型字符 iscntrl() 检查是否为控制字符 ispunct() 检查是否为标点符号 isalnum() 检查是否为字母和数字 isprint() 检查是否是可打印字符 isgraph() 检查是否是图形字符,等效于 isalnum() | ispunct()

4)函数原型

原型:strcpy(char destination[], const char source[]);

功能:将字符串source拷贝到字符串destination中

例程:

复制代码
复制代码
 #include
  #include
  void main(void)
  char str1[10] = { "TsinghuaOK"};
  char str2[10] = { "Computer"};
  cout <<strcpy(str1,str2)<<endl;< p="">
  }
复制代码
复制代码

运行结果是:Computer

第二个字符串将覆盖掉第一个字符串的所有内容!

注意:在定义数组时,字符数组1的字符串长度必须大于或等于字符串2的字符串长度。不能用赋值语句将一个字符串常量或字符数组直接赋给一个字符数组。所有字符串处理函数都包含在头文件string.h中。

strncpy(char destination[], const char source[], int numchars); strncpy:将字符串source中前numchars个字符拷贝到字符串destination中。 strncpy函数应用举例 原型:strncpy(char destination[], const char source[], int numchars); 功能:将字符串source中前numchars个字符拷贝到字符串destination中

例程:

复制代码
复制代码
  #include
  #include
  void main(void)
  char str1[10] = { "Tsinghua "};
  char str2[10] = { "Computer"};
  cout <<strncpy(str1,str2,3)<<endl;< p="">
  }
复制代码
复制代码

运行结果:Comnghua

注意:字符串source中前numchars个字符将覆盖掉字符串destination中前numchars个字符!

原型:strcat(char target[], const char source[]);

功能:将字符串source接到字符串target的后面

例程:

复制代码
复制代码
  #include
  #include
  void main(void)
  char str1[] = { "Tsinghua "};
  char str2[] = { "Computer"};
  cout <<strcpy(str1,str2)<<endl;< p="">
  }
复制代码
复制代码

运行结果:Tsinghua Computer

注意:在定义字符数组1的长度时应该考虑字符数组2的长度,因为连接后新字符串的长度为两个字符串长度之和。进行字符串连接后,字符串1的结尾符将自动被去掉,在结尾串末尾保留新字符串后面一个结尾符。

原型:strncat(char target[], const char source[], int numchars);

功能:将字符串source的前numchars个字符接到字符串target的后面

例程:

复制代码
复制代码
  #include
  #include
  void main(void)
  char str1[] = { "Tsinghua "};
  char str2[] = { "Computer"};
  cout <<strncat(str1,str2,3)<<endl;< p="">
  }
复制代码
复制代码

运行结果:Tsinghua Com

原型:int strcmp(const char firststring[], const char secondstring);

功能:比较两个字符串firststring和secondstring

例程:

复制代码
复制代码
  #include
  #include
  void main(void)
  char buf1[] = "aaa";
  char buf2[] = "bbb";
  char buf3[] = "ccc";
  int ptr;
  ptr = strcmp(buf2,buf1);
  if(ptr > 0)
  cout <<"Buffer 2 is greater than buffer 1"<<endl;< p="">
  cout <<"Buffer 2 is less than buffer 1"<<endl;< p="">
  ptr = strcmp(buf2,buf3);
  if(ptr > 0)
  cout <<"Buffer 2 is greater than buffer 3"<<endl;< p="">
  cout <<"Buffer 2 is less than buffer 3"<<endl;< p="">
  }
复制代码
复制代码

运行结果是:Buffer 2 is less than buffer 1

Buffer 2 is greater than buffer 3

原型:strlen( const char string[] );

功能:统计字符串string中字符的个数

例程:

复制代码
复制代码
 #include
  #include
  void main(void)
  char str[100];
  cout <<"请输入一个字符串:";
 
推荐文章
活泼的打火机  ·  ubuntu搭建sftp服务 - 小伍子 - 博客园
1 年前
豪气的小摩托  ·  【数据库原理及应用】创建与使用触发器-阿里云开发者社区
2 年前
老实的领结  ·  docker alpine镜像中遇到 sh: xxx: not found_辣椒炒肉真好吃的博客-CSDN博客
2 年前
奔跑的猴子  ·  盘点一份JS逆向代码转换为Python代码的教程-python货币转换程序代码
2 年前
高大的椰子  ·  人流量统计抓帧的时候,frame为空
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号