C语言---常用的函数
来源:互联网 发布:特种兵选吧 知乎 编辑:程序博客网 时间:2024/06/01 22:51
1.拷贝函数
介绍:C语言标准库函数strcpy,把从src地址开始且含有'\0'结束符的字符串复制到以dest开始的地址空间;
原型声明:char *strcpy(char* dest, const char *src);
头文件:#include <string.h> 和 #include <stdio.h>
功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串,返回指向dest的指针;
#include "string.h"#include "stdio.h"int main(){char a[]="hello";char b[]="world";char c[10];strcpy(c,a);printf("%s\n",c);strcpy(c,b);printf("%s\n",c);return 0;}输出结果为:
从中可以看出,首先,strcpy的第二个参数应该为常量字符串,然后呢,*src字符串覆盖掉了,*dest前面的字符串,所以最后输出的只有*src的字符串,还要啊,这是个void型函数,没有返回值的哦!
2.拼接函数
原型:extern char *strcat(char *dest,char *src);
头文件: #include <string.h> 和 #include <stdio.h>
功能把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')。
说明src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
头文件: #include <string.h> 和 #include <stdio.h>
功能把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')。
说明src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
#include "string.h"#include "stdio.h"int main(){char a[]="hello";char b[]="world";//char c[10]="tttttt";strcat(a,b);printf("%s\n",a);return 0;}
3.字符串长度函数
原型:unsigned int strlen(const char *s)
头文件: #include <string.h> 和 #include <stdio.h>
Get the length of a string, using the current locale or a specified locale.
计算字符串s的(unsigned int型)长度,不包括'\0'在内。返回 s 的长度,不包括结束符NULL。
头文件: #include <string.h> 和 #include <stdio.h>
Get the length of a string, using the current locale or a specified locale.
计算字符串s的(unsigned int型)长度,不包括'\0'在内。返回 s 的长度,不包括结束符NULL。
strlen和sizeof()的区别:
strlen(char*)函数求的是字符串的实际长度,它求得方法是从开始到遇到第一个'\0',如果你只定义没有给它赋初值,这个结果是不定的,它会从aa首地址一直找下去,直到遇到'\0'停止。
而sizeof()返回的是变量声明后所占的内存数,不是实际长度,此外sizeof不是函数,仅仅是一个操作符,strlen是函数。
注意:strlen后面只能接char *,且必须是以'\0'结尾的,不能求出 int *类型的长度;
4.比较字符串函数
原型:int strcmp(const char *s1,const char * s2);
#include<string.h>
Compare strings.
< 0 :s1 less than s2
= 0 : s1 identical s2
> 0 : s1 greater than s2
两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。这里面只能比较字符串,不能比较数字等其他形式的参数。
#include "string.h"#include "stdio.h"int main(){int n;char a[]="hello";char b[]="world";char c[]="hello";char d[]="helloworld";n=strcmp(a,b);printf("strcmp(a,b)=%d\n",n);n=strcmp(a,c);printf("strcmp(a,c)=%d\n",n);n=strcmp(a,d);printf("strcmp(a,d)=%d\n",n);return 0;}
strcmp(a,b)=-1strcmp(a,c)=0strcmp(a,d)=-1
5.查找字符函数
原型: char *strchr(const char *s,char c);
#include<string.h>
查找字符串s中首次出现字符c的位置,返回首次出现c的位置的指针,如果s中不存在c则返回NULL。
The strchr function finds the first occurrence of c instr, or it returns NULL ifc is not found. The null terminating character is included in the search.
常用玩法:查找出某个字符,用指针变量存储返回值,再用 *p,将其替换成其他有用的字符~
#include "string.h"#include "stdio.h"int main(){char a[]="helloworld";char *p ;p=strchr(a,'l');*p='\0';printf("%s",a);return 0;}
输出结果为:
-------------------------------------------------------------------------------------------------------------------
与之对应的函数:
原型:char *strrchr(const char *str, char c);
#include<string.h>
找一个字符c在另一个字符串str中末次出现的位置(也就是从str的右侧开始查找字符c首次出现的位置),并返回从字符串中的这个位置起,一直到字符串结束的所有字符。如果未能找到指定字符,那么函数将返回NULL。
#include "string.h"#include "stdio.h"int main(){char a[]="helloworld";char *p ;p=strrchr(a,'l');*p='\0';printf("%s",a);return 0;}
输出结果为:
6.查找子字符串函数
原型:char *strstr(const char *str1, const char *str2);
#include<string.h>
找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。返回该位置的指针,如找不到,返回空指针;
#include "string.h"#include "stdio.h"int main(){char a[]="helloworld";char b[]="low" ;char *p;p=strstr(a,b);*p='\0';printf("%s",a);return 0;}
输出结果为:
7.查找未出现字符
原型: size_t strspn (const char *s,const char * accept);
#include<string.h>
strspn返回 s 中第一个不在accept中出现的字符下标。,返回的是一个整型!
#include "string.h"#include "stdio.h"int main(){char a[]="hey";char b[]="hello";int p=strspn(a,b);printf("%d",p);return 0;}
8.选择字符个数复制
原型:char * strncpy(char *dest, char *src, size_t n);
#include<string.h>
功能:将字符串src中最多n个字符复制到字符数组dest中(它并不像strcpy一样只有遇到NULL才停止复制,而是多了一个条件停止,就是说如果复制到第n个字符还未遇到NULL,也一样停止),返回指向dest的指针。
#include "string.h"#include "stdio.h"int main(){char a[]="hey";char b[]="hello";strncpy(a,b,3);printf("%s",a);return 0;}
9.选择字符个数进行拼接
原型: char *strncat(char *dest,const char *src,int n);
#include<string.h>
功能:把src所指字符串的前n个字符添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
#include "string.h"#include "stdio.h"int main(){char a[]="hey";char b[]="hello";strncat(a,b,3);printf("%s",a);return 0;}
0 0
- C语言常用的函数
- C语言---常用的函数
- C语言的常用函数
- C语言常用函数
- C语言常用函数
- C语言常用函数
- c 语言 常用函数
- C语言 常用函数
- C语言常用函数
- C语言常用函数的包含文件
- 一些常用的C语言函数源代码
- C语言的常用类型转换函数
- Vuser常用的c语言函数
- c语言中一些常用的函数
- 几个常用C语言函数的分析
- c语言的常用字符串函数
- LoadRunner中常用的C语言函数
- c语言中字符串的常用函数
- 质数
- JS中Array方法中常用方法一:Array.isArray():
- NYOJ 21 三个水杯(BFS)
- React Native环境配置 —— window版
- char与byte的区别
- C语言---常用的函数
- 内容提供者 Content Provider - 基本知识
- 3G卡片在开发板上的详细解决方法(适用于大多数3G卡片)
- PS模仿欢乐颂电视剧海报的水彩效果
- HDU 5876 Sparse Graph
- jquery mobile mobileinit事件
- 环境传感器的组成及使用方法
- eclipse中如何为类快速添加多个构造方法
- ImgageLoader学习总结