当前位置:网站首页>[C language] some commonly used string functions -- learning notes
[C language] some commonly used string functions -- learning notes
2022-07-21 20:18:00 【LiangWF22】
1、strcat(buf, text);
effect : Used to connect two strings
Use cases , Connect buf+text:
#include <stdio.h>
#include <string.h>
int main ()
{
char src[50], dest[50];
strcpy(src, "This is source");
strcpy(dest, "This is destination");
strcat(dest, src);
printf(" The final target string : |%s|", dest);
return(0);
}
2、strcpy()
strcpy() The function is C A library function in the language that copies strings
Use cases , take c Copied to the a:
#include<string.h>
#include <stdio.h>
void main()
{
char a[20], c[] = "I am a teacher!";
strcpy(a, c);
printf(" c=%s\n", c);
printf(" a=%s\n", a);
}
3、sprintf()
sprintf() Function is to print content into buffer[]
Use cases :
#include<string.h>
#include <stdio.h>
int main()
{
int ddd=666;
char *buffer=NULL;
if((buffer = (char *)malloc(80*sizeof(char)))==NULL)
{
printf("malloc error\n");
}
sprintf(buffer, "The value of ddd = %d", ddd);//The value of ddd = 666
printf("%s\n",buffer);
free(buffer);
buffer=NULL;
return 0;
}
4、strstr()
Look for the first time “IPD” The location of the sign ,
such as :strstr(buff, “IPD,”); // stay buf Mid search “IPD” head
#include <stdio.h>
#include <string.h>
int main()
{
const char buff[20] = "WWW.IPD:hello";
const char need[10] = "IPD";
char *ret;
ret = strstr(haystack, need);
printf(" The substring is : %s\n", ret);
return(0);
}
The function returns in buff For the first time need The location of the string , If not found, return null
5、strchr()
strchr() The function searches for the first occurrence of a string in another string
such as :strchr(buff, ‘:’); // stay buff Find ’:’
#include <stdio.h>
#include <string.h>
int main ()
{
const char buff[] = "IPD:liangwwff ";
const char ch = ':';
char *ret;
ret = strchr(buff, ch);
printf("|%c| The following string is - |%s|\n", ch, ret);
return(0);
}
This function returns the string str The first occurrence of the character c The location of , If the character is not found, returns NULL.
6、strlen()
strlen() Function to find the length of a string ( How many characters are included )
#include <stdio.h>
#include <string.h>
void main()
{
char str[100] = {
0 };
size_t len;
gets(str);
len = strlen(str);
printf("Length: %d\n", len);
}
边栏推荐
- 【记录】Optisystem运行卡死,无法点击关闭、输入变量数值等问题解决方法
- Development tools supporting data + code generation, yyds
- How do I optimize from 20s to 500ms
- fastjson @JSONField format 不生效的原因
- isEmpty与 isBlank
- Classloader and parental delegation mechanism
- APISIX微服务网关
- 学习IO由浅入深
- 回型输入和回型输出
- Jenkins plug-in development - provide external access interface
猜你喜欢
图的邻接表及其深度优先(DFS)、广度优先(BFS)遍历
Share the experience of big factories, come quickly
[untitled]
Initializing libiomp5.dylib, but found libomp.dylib already initialized
flutter自定义form表单,封装form表单组件
Development tools supporting data + code generation, yyds
【PCB】基于合泰HT32F52352芯片电路板绘制实验(WiFi及光传感模块)-画板笔记
As editor sets the indent position of the code on the right side of fluent development
FL Studio 20.9 Chinese patch package for fruit composition software
【3D建模】Solidworks 3D建模及PrusaSlicer切片打印学习笔记
随机推荐
pytorch安装
Database design introduction to database system (Fifth Edition)
【404】服务器启动成功,默认页面无法访问
拉取项目pom文件报错,jai_core-1.1.3.jar,解决jar引入问题
分布式事务其中的那些坑
[404] the server starts successfully, and the default page cannot be accessed
以独占的方式锁定此配置文件失败。另一个正在运行的VMware进程可能正在使用配置文件。
NIO之Channel详解
el-radio值无法回显
Web. Config custom class reading
Bert原理概述
Three paradigms of database design in MySQL
M进制转换为N进制
对象拷贝工具类(fastjson)
Single project - Ruiji takeout
微信公眾號開發接入,利用微信公眾平臺申請測試號進行本地開發
【PCB】Altium Designer 中(Via过孔)(文本string)(走线Linear Dimension)等项目修改默认设置
【GeoServer二次开发】基于GeoServer-Manager的REST自动部署模块开发
Classloader and parental delegation mechanism
conda安装datasets避免冲突命令