当前位置:网站首页>PZK学C语言之字符串函数(一)
PZK学C语言之字符串函数(一)
2022-07-27 05:20:00 【未来Linux驱动工程师PZK】
一、字符串函数:
1.字符串比较函数 strcmp
函数原型:int strcmp(const char *s1, const char *s2);
作 用: 比较s1和s2字符串是否相同
参 数:s1是需要比较的第一个字符串
s2是需要比较的第二个字符串
返 回: 0 如果两个字符串相同
<0 第一个字符串小于第二个字符串
>0 第一个字符串大于第二个字符串
比较的是第一个不相同字符的ascii的值的差。
#include <stdio.h>
#include <string.h>
#include <strings.h>
int main()
{
char str1[20];//定义字符串
char str2[20];
int temp;
printf("请输入两个字符串\n");
gets(str1);//从键盘获取输入的字符串
gets(str2);
temp = strcmp(str1,str2);//获取strcmp的返回值
printf("%d",temp);
return 0;
}
2.得到字符串长度 strlen
函数原型: size_t strlen(const char *s);
作用: 得到字符串的有效长度
参数: 需要获取长度的字符串内容
返回: s字符串的有效长度
#include <stdio.h>
#include <string.h>
#include <strings.h>
int main()
{
char str1[20];//定义字符串
int temp;
printf("请输入字符串\n");
gets(str1);//获取输入
temp = strlen(str1);//strlen的返回值是返回字符串的有效长度,不包括\0;
printf("%d",temp);
return 0;
}
3.字符串追加 strcat
函数原型:char *strcat(char *dest, const char *src);
作用:把src字符串拼接到dest字符串后面
参数: src源字符串,需要拼接到dest后的字符串
dest目的字符串,需要存放src字符串。
返回:返回拼接后的字符串内容。
#include <stdio.h>
#include <string.h>
#include <strings.h>
int main()
{
char str1[20];
char str2[20];
memset(str1,1,sizeof(str1));//初始化字符串,可以不用写,写了更好;
printf("arr[0] = %d\n",str1[0]);
printf("请输入两个字符串\n");
gets(str1);
gets(str2);
if(strncmp(str1,str2,5) == 0)//比较str1和str2的前五个字符
{
strncat(str1,str2,3);//追加,把str2的前三个字符追加到str1中
}
puts(str1);
return 0;
}
4.字符串的拼接 sprintf
函数原型:int printf(const char *format, …);
作用:把可变长参数中的内容合并成一个字符串format
参数:第一个部分:字符串变量,
第二个部分: 双引号的内容,有占位符
第三个部分:各种变量,用来填充占位。
返回:拼接后的字符串长度。。
#include <stdio.h>
#include <string.h>
int main(int argc ,char* argv[])
{
char arr1[20] = "abcdf";
char arr2[] = "abcdf";
printf("arr1 = %s\n",arr1);
int a = 10;
int b = 20;
int temp;
temp=sprintf(arr1,"daniubi-%d-%d",a,b);
//把a和b变量放到占位符里面,然后把整个字符串赋给arr1,会覆盖掉arr1原先的内容;
printf("arr1=%s\n temp =%d\n",arr1,temp);
system("pause");
return 0;
}
5.字符串的输入
char *gets(char *s);
作用:获取一个字符串,以回车结束输入。
参数:输入后的内容保存的位置。
返回值:输入的字符串内容。
注意:没法检测s的内存空间,避免使用。
#include <stdio.h>
#include <string.h>
int main()
{
printf("输入一个字符串\n");
char str[20]= {
0};
gets(str);//从键盘输入一个字符串
printf("str = %s\n",str);
return 0;
}
int getchar(void);
作用:获取一个输入的字符
参数: 无参数
返回值:输入的字符的ascii值。
注意:输入函数是堵塞函数,没有输入则一直等待直到输入结束为止。
#include <stdio.h>
#include <string.h>
int main()
{
printf("输入一个字符\n");
getchar();//从键盘获取一个字符
int temp = getchar();
printf("temp = %d",temp);
return 0;
}
6.字符串的输出
int putchar(int c);
作用:输出一个字符
参数:需要输出的字符ascii值
返回:也是输出的ascii值
int puts(const char *s);
作用:输出一个字符串
参数: 需要输出的字符串内容
返回值:输出的字符串的字符个数,包含\0
#include <stdio.h>
#include <string.h>
int main()
{
printf("输入一个字符串\n");
char str[20]= {
0};
gets(str);
puts(str);
return 0;
}
7.字符串的拷贝
char *strcpy(char *dest, const char *src);
作用:拷贝src的内容到dest中
参数: src源字符串
dest 目的字符串
返回值:拷贝好后的字符串 dest字符串。
char *strncpy(char *dest, const char *src, size_t n);
作用:拷贝src中的前n个字符。
参数: src源字符串
dest 目的字符串
n 需要拷贝的字符串个数
返回值:拷贝好后的字符串 dest字符串。
边栏推荐
- 方差与协方差
- Gbase 8C - SQL reference 6 SQL syntax (12)
- C language - linear sequence table
- Redis在windows下的idea连接不上问题
- [song] rebirth of me in py introductory training (7): function call
- Greedy high performance neural network and AI chip application research and training
- pytorch中交叉熵损失函数的细节
- Pix2Pix原理解析
- 19. Up and down sampling and batchnorm
- pytorch使用data_prefetcher提升数据读取速度
猜你喜欢

发布 分辨率0.22m的建筑物分割数据库

16. Over fitting and under fitting

Super remote connection management tool: Royal TSX

6. Dimension transformation and broadcasting

10. Gradient, activation function and loss

为什么交叉熵损失可以用于刻画损失

导数、偏导数以及梯度

1. Introduction to pytorch

Greedy high performance neural network and AI chip application research and training

图像超分辨率评价指标
随机推荐
向量和矩阵的范数
文件的路径
arcgis for js api(1) 获取featureLayer的所有字段名
socket编程二:使用select
使用Markdowm
方差与协方差
【5·20特辑】MatLAb之我在和你表白
神经网络参数初始化
[concurrent programming series 9] priorityblockingqueue, delayqueue principle analysis of blocking queue
图像超分辨率评价指标
Multi task foundation of IOT operating system
What has been updated in the Chinese version of XMIND mind map 2022 v12.0.3?
【12】理解电路:从电报机到门电路,我们如何做到“千里传信”?
超强远程连接管理工具:Royal TSX
Weidongshan digital photo frame project learning (IV) simple TXT document display (e-paper book)
Gbase 8C - SQL reference 6 SQL syntax (14)
Weidongshan digital photo frame project learning (III) transplantation of freetype
【第一篇博客-展望】
arcgis for js api(2) 获取要素服务的id集合
15. GPU acceleration, Minist test practice and visdom visualization