当前位置:网站首页>Pzk learns string function of C language (1)
Pzk learns string function of C language (1)
2022-07-27 06:09:00 【Future Linux Driver Engineer pzk】
One 、 String function :
List of articles
1. String comparison function strcmp
The function prototype :int strcmp(const char *s1, const char *s2);
do use : Compare s1 and s2 Is the string the same
ginseng Count :s1 Is the first string to compare
s2 Is the second string to compare
return return : 0 If two strings are the same
<0 The first string is less than the second string
>0 The first string is larger than the second string
The comparison is the first different character ascii The difference between the values of .
#include <stdio.h>
#include <string.h>
#include <strings.h>
int main()
{
char str1[20];// Define string
char str2[20];
int temp;
printf(" Please enter two strings \n");
gets(str1);// Get the input string from the keyboard
gets(str2);
temp = strcmp(str1,str2);// obtain strcmp The return value of
printf("%d",temp);
return 0;
}
2. Get the string length strlen
The function prototype : size_t strlen(const char *s);
effect : Get the effective length of the string
Parameters : You need to get the string content of length
return : s The effective length of the string
#include <stdio.h>
#include <string.h>
#include <strings.h>
int main()
{
char str1[20];// Define string
int temp;
printf(" Please enter the string \n");
gets(str1);// Get input
temp = strlen(str1);//strlen The return value of is the effective length of the return string , barring \0;
printf("%d",temp);
return 0;
}
3. String append strcat
The function prototype :char *strcat(char *dest, const char *src);
effect : hold src String concatenation to dest After the string
Parameters : src The source string , It needs to be spliced to dest String after
dest Destination string , Need to store src character string .
return : Return the string content after splicing .
#include <stdio.h>
#include <string.h>
#include <strings.h>
int main()
{
char str1[20];
char str2[20];
memset(str1,1,sizeof(str1));// Initialize string , You don't have to write , It's better to write ;
printf("arr[0] = %d\n",str1[0]);
printf(" Please enter two strings \n");
gets(str1);
gets(str2);
if(strncmp(str1,str2,5) == 0)// Compare str1 and str2 The first five characters of
{
strncat(str1,str2,3);// Additional , hold str2 The first three characters of are appended to str1 in
}
puts(str1);
return 0;
}
4. String splicing sprintf
The function prototype :int printf(const char *format, …);
effect : Combine the contents of variable length parameters into a string format
Parameters : The first part : String variable ,
Second part : The contents of double quotes , There are placeholders
The third part : Various variables , Used to fill the placeholder .
return : String length after splicing ..
#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);
// hold a and b Put variables in placeholders , Then assign the whole string to arr1, Will overwrite arr1 The original content ;
printf("arr1=%s\n temp =%d\n",arr1,temp);
system("pause");
return 0;
}
5. Input of string
char *gets(char *s);
effect : Get a string , End with enter .
Parameters : Where to save the entered content .
Return value : The contents of the input string .
Be careful : Can't detect s Of memory space , Avoid using .
#include <stdio.h>
#include <string.h>
int main()
{
printf(" Enter a string \n");
char str[20]= {
0};
gets(str);// Enter a string from the keyboard
printf("str = %s\n",str);
return 0;
}
int getchar(void);
effect : Get an input character
Parameters : No parameter
Return value : Of the characters entered ascii value .
Be careful : The input function is a blocking function , If there is no input, wait until the input ends .
#include <stdio.h>
#include <string.h>
int main()
{
printf(" Enter a character \n");
getchar();// Get a character from the keyboard
int temp = getchar();
printf("temp = %d",temp);
return 0;
}
6. String output
int putchar(int c);
effect : Output a character
Parameters : Characters to be output ascii value
return : Also output ascii value
int puts(const char *s);
effect : Output a string
Parameters : The string content that needs to be output
Return value : The number of characters of the output string , contain \0
#include <stdio.h>
#include <string.h>
int main()
{
printf(" Enter a string \n");
char str[20]= {
0};
gets(str);
puts(str);
return 0;
}
7. Copy of string
char *strcpy(char *dest, const char *src);
effect : Copy src Content to dest in
Parameters : src The source string
dest Destination string
Return value : The copied string dest character string .
char *strncpy(char *dest, const char *src, size_t n);
effect : Copy src In front of n Characters .
Parameters : src The source string
dest Destination string
n The number of strings to be copied
Return value : The copied string dest character string .
边栏推荐
- 力扣题解 单调栈
- [song] rebirth of me in py introductory training (7): function call
- [song] rebirth of me in py introductory training (9): exception handling
- 对于windows下的Redis,只能读不能写的问题
- [first song] rebirth of me in py introductory training (1)
- Leetcode one question per day 30. Concatenate substrings of all words
- 安全帽反光衣检测识别数据集和yolov5模型
- Gbase 8C - SQL reference 6 SQL syntax (14)
- ps 2022 六月更新,都新增了哪些功能
- Unity Hub登录无响应
猜你喜欢

2021-06-26

根据SQL必知必会学习SQL(MYSQL)

论文报告-Linear Regression for face recognition

WebODM win10安装教程(亲测)

性感素数(Acwing每日一题)

对于windows下的Redis,只能读不能写的问题

The principle of hash table and the solution of hash conflict

Cesium教程 (1) 界面介绍-3dtiles加载-更改鼠标操作设置

C语言扫雷最新 递归展开 超详解(附源码)

Summary of the use of C # Jason code in TCP communication
随机推荐
力扣题解 二叉树(6)
PZK学C语言之初识指针
Reading and writing of C # file
Unity 菜单界面的简单介绍
Pycharm installation and import project considerations
制作视频后期特效需要什么工具?
QGIS系列(1)-QGIS(server-apache) win10安装
解决conda install 安装停止、中断问题
Solve binary tree (6)
浅记一下十大排序
SQL初识
Code implementation and introduction of all commonly used sorting
[song] rebirth of me in py introductory training (12): Matplotlib interface and common graphics
socket 长链接
Osg环境搭建(Win10+Vs2019)
哈夫曼树的求法,代码实现及证明,图文解释
Day 2. Depressive symptoms, post-traumatic stress symptoms and suicide risk among graduate students
WebODM win10安装教程(亲测)
安全帽反光衣检测识别数据集和yolov5模型
Leetcode每日一题30. 串联所有单词的子串