博客
关于我
【C语言】字符函数与字符串函数介绍及使用
阅读量:501 次
发布时间:2019-03-07

本文共 4264 字,大约阅读时间需要 14 分钟。

C语言字符串处理函数及其他相关函数教程

1. strlen

size_t strlen(const char* str);

功能说明:

strlen函数用于计算字符串的长度。
字符串在C语言中以 '\0' 结束,strlen函数返回 '\0' 前面的字符个数。

示例代码:

#include 
#include
int main() { const char* str = "abcdef"; printf("%d\n", strlen(str)); return 0;}

执行结果:

输出:6


2. strcpy

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

功能说明:

strcpy函数用于将源字符串拷贝到目标空间中。
目标空间必须足够大且可变,且源字符串必须以 '\0' 结束。

示例代码:

#include 
#include
int main() { char str1[] = "Sample string"; char str2[40]; char str3[40]; strcpy(str2, str1); strcpy(str3, "copy successful"); printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3); return 0;}

执行结果:

输出:
str1: Sample string
str2: Sample string
str3: copy successful


3. strcat

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

功能说明:

将源字符串追加到目标字符串中。
源字符串必须以 '\0' 结束,目标空间必须足够大,能容纳源字符串的内容。

示例代码:

#include 
#include
int main() { char str[80]; strcpy(str, "these "); strcat(str, "strings "); strcat(str, "are "); strcat(str, "concatenated."); puts(str); return 0;}

执行结果:

输出:these strings are concatenated.


4. strcmp

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

功能说明:

比较两个字符串的大小关系。
返回值:

  • 大于0:str1大于str2
  • 等于0:str1等于str2
  • 小于0:str1小于str2

示例代码:

#include 
#include
int main() { char key[] = "apple"; char buffer[80]; do { printf("Guess my favorite fruit? "); fflush(stdout); scanf("%79s", buffer); } while (strcmp(key, buffer) != 0); puts("Correct answer!"); return 0;}

执行结果:

输出:
Guess my favorite fruit? orange
Guess my favorite fruit? apple
Correct answer!


5. strstr

const char* strstr(const char* str1, const char* str2);

功能说明:

判断字符串str2是否是str1的子串。
如果是,返回str2在str1中首次出现的地址;否则返回NULL。

示例代码:

#include 
#include
int main() { char str[] = "This is a simple string"; char* pch; pch = strstr(str, "simple"); if (pch != NULL) { strncpy(pch, "sample", 6); } puts(str); return 0;}

执行结果:

输出:This is a sample string


6. strtok

char* strtok(char* str, const char* delimiters);

功能说明:

将字符串按照指定的分隔符分割成多个子串。
第一次调用时,str必须指向有效字符串,后续调用时,str可设为NULL,继续分割。

示例代码:

#include 
#include
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.


7. strerror

char* strerror(int errnum);

功能说明:

返回一个指针,该指针指向描述错误码errnum的静态字符串。
程序不得修改该字符串。

示例代码:

#include 
#include
#include
int main() { FILE* pFile; pFile = fopen("unexist.ent", "r"); if (pFile == NULL) { printf("Error opening file unexist.ent: %s\n", strerror(errno)); } return 0;}

执行结果:

输出:打开文件不存在时出错:没有这样的文件或目录


字符分类函数

函数 描述
isdigit 判断字符是否为十进制数字(0-9)
islower 判断字符是否为小写字母(a-z)
isupper 判断字符是否为大写字母(A-Z)
isalpha 判断字符是否为字母(a-z或A-Z)
isalnum 判断字符是否为字母或数字(a-z, A-Z, 0-9)
isspace 判断字符是否为空白字符(space, tab等)

字符转换函数

函数 作用
tolower 将字符转换为小写字母
toupper 将字符转换为大写字母

10. memcpy

void* memcpy(void* destination, const void* source, size_t num);

功能说明:

从source位置开始,向目标地址destination拷贝num个字节的数据。
与strcpy不同, memcpy不会关注源字符串的 '\0'。

示例代码:

#include 
#include
struct { char name[40]; int age;} person, person_copy;int main() { char myname[] = "Pierre de Fermat"; memcpy(person.name, myname, strlen(myname) + 1); person.age = 46; memcpy(&person_copy, &person, sizeof(person)); printf("person_copy: %s, %d \n", person_copy.name, person_copy.age); return 0;}

执行结果:

输出:person_copy: Pierre de Fermat, 46


11. memmove

void* memmove(void* destination, const void* source, size_t num);

功能说明:

与memcpy类似,但可以处理源内存块和目标内存块重叠的情况。

示例代码:

#include 
#include
int main() { char str[] = "memmove can be very useful......"; memmove(str + 20, str + 15, 11); puts(str); return 0;}

执行结果:

输出:memmove can be very very useful.


12. memcmp

void* memcmp(void* ptr1, const void* ptr2, size_t num);

功能说明:

与strcmp类似,但会比较整个num个字节,而不是在遇到 '\0' 时停止。
返回值:

  • < 0:ptr1中的第一个字节小于ptr2中的第一个字节
  • = 0:两个内存块内容相等
  • 0:ptr1中的第一个字节大于ptr2中的第一个字节

转载地址:http://ssacz.baihongyu.com/

你可能感兴趣的文章
nodejs框架,原理,组件,核心,跟npm和vue的关系
查看>>
Nodejs概览: 思维导图、核心技术、应用场景
查看>>
nodejs模块——fs模块
查看>>
Nodejs模块、自定义模块、CommonJs的概念和使用
查看>>
nodejs生成多层目录和生成文件的通用方法
查看>>
nodejs端口被占用原因及解决方案
查看>>
Nodejs简介以及Windows上安装Nodejs
查看>>
nodejs系列之express
查看>>
nodejs系列之Koa2
查看>>
Nodejs连接mysql
查看>>
nodejs连接mysql
查看>>
NodeJs连接Oracle数据库
查看>>
nodejs配置express服务器,运行自动打开浏览器
查看>>
NodeMCU教程 http请求获取Json中文乱码解决方案
查看>>
Nodemon 深入解析与使用
查看>>
NodeSession:高效且灵活的Node.js会话管理工具
查看>>
node~ http缓存
查看>>
node不是内部命令时配置node环境变量
查看>>
node中fs模块之文件操作
查看>>
Node中同步与异步的方式读取文件
查看>>