c库函数——字符串函数
来源:互联网 发布:js中srcelement是什么 编辑:程序博客网 时间:2024/06/04 19:50
Code:
char stpcpy(char*dest,const char *src)
将字符串src复制到dest
char strcat(char*dest,const char *src)
将字符串src添加到dest末尾
char strchr(constchar *s,int c)
检索并返回字符c在字符串s中第一次出现的位置
int strcmp(const char *s1,const char *s2)
比较字符串s1与s2的大小,并返回s1-s2
char strcpy(char*dest,const char *src)
将字符串src复制到dest
size_t strcspn(const char *s1,const char *s2)
扫描s1,返回在s1中有,在s2中也有的字符个数
char strdup(constchar *s)
将字符串s复制到最近建立的单元
int stricmp(const char *s1,const char *s2)
比较字符串s1和s2,并返回s1-s2
size_t strlen(const char *s)
返回字符串s的长度
char strlwr(char*s)
将字符串s中的大写字母全部转换成小写字母,并返回转换后的字符串
char strncat(char*dest,const char *src,size_t maxlen)
将字符串src中最多maxlen个字符复制到字符串dest中
int strncmp(const char *s1,const char *s2,size_t maxlen)
比较字符串s1与s2中的前maxlen个字符
char strncpy(char*dest,const char *src,size_t maxlen)
复制src中的前maxlen个字符到dest中
int strnicmp(const char *s1,const char *s2,size_t maxlen)
比较字符串s1与s2中的前maxlen个字符
char strnset(char*s,int ch,size_t n)
将字符串s的前n个字符置于ch中
char strpbrk(const char *s1,const char *s2)
扫描字符串s1,并返回在s1和s2中均有的字符个数
char strrchr(const char *s,int c)
扫描最后出现一个给定字符c的一个字符串s
char strrev(char*s)
将字符串s中的字符全部颠倒顺序重新排列,并返回排列后的字符串
char strset(char*s,int ch)
将一个字符串s中的所有字符置于一个给定的字符ch
size_t strspn(const char *s1,const char *s2)
扫描字符串s1,并返回在s1和s2中均有的字符个数
char strstr(constchar *s1,const char *s2)
扫描字符串s2,并返回第一次出现s1的位置
char strtok(char*s1,const char *s2)
检索字符串s1,该字符串s1是由字符串s2中定义的定界符所分隔
char strupr(char*s)
将字符串s中的小写字母全部转换成大写字母,并返回转换后的字符串
详细例子
函数名: stpcpy
功 能: 拷贝一个字符串到另一个
用 法: char *stpcpy(char *destin, char*source);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
charstring[10];
char *str1 ="abcdefghi";
stpcpy(string,str1);
printf("%s\n",string);
return 0;
}
函数名: strcat
功 能: 字符串拼接函数
用 法: char *strcat(char *destin, char*source);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
chardestination[25];
char *blank = "", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf("%s\n",destination);
return 0;
}
函数名: strchr
功 能: 在一个串中查找给定字符的第一个匹配之处\
用 法: char *strchr(char *str, charc);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char string[15];
char *ptr, c = 'r';
strcpy(string, "This is a string");
ptr = strchr(string, c);
if (ptr)
printf("The character %c is at position: %d\n", c,ptr-string);
else
printf("The character was not found\n");
return 0;
}
函数名: strcmp
功 能: 串比较
用 法: int strcmp(char *str1, char*str2);
看Asic码,str1>str2,返回值 >0;两串相等,返回0
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");
ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");
return 0;