string重点函数集合

<**\\**\重点函数集合\**\\**\>>

1函数名:strcpy

功能
:拷贝一个字符串到另一个

用法:char strcpy(char destin, char *source);

程序例:

#include

#include

intmain(void)

{

char string[10];

char *str1 = "abcdefghi";

strcpy(string,str1);

printf("%sn", string);

return 0;

}

2函数名:strcat

功能
:字符串拼接函数

用法:char strcat(char destin, char *source);

程序例:

#include

#include

intmain(void)

{

char destination[25];

char blank = " ", c = "C++", *Borland ="Borland";

strcpy(destination,Borland);

strcat(destination, blank);

strcat(destination, c);

printf("%sn",destination);

return 0;

}

3函数名:strchr

功能
:在一个串中查找给定字符的第一个匹配之处

用法:char strchr(char str, char c);

程序例:

#include

#include

intmain(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: %dn", c,ptr-string);

else

printf("The character was not foundn");

return 0;

}

4函数名:strcmp

功能
:串比较

用法:int strcmp(char str1, char str2);

Asic码,str1>str2,返回值>0;两串相等,返回0

程序例:

#include

#include

intmain(void)

{

char buf1 = "aaa", buf2 = "bbb",*buf3 = "ccc";

int ptr;

ptr= strcmp(buf2, buf1);

if (ptr > 0)

printf("buffer2 is greater than buffer 1n");

else

printf("buffer 2 is less than buffer 1n");

ptr= strcmp(buf2, buf3);

if (ptr > 0)

printf("buffer 2 is greater than buffer 3n");

else

printf("buffer 2 isless than buffer 3n");

return0;

}

5函数名:strncmpi

功能
:将一个串中的一部分与另一个串比较,不管大小写

用法:int strncmpi(char str1, char str2, unsigned maxlen);

程序例:

#include

#include

intmain(void)

{

char buf1 = "BBB", buf2= "bbb";

int ptr;

ptr= strcmpi(buf2, buf1);

if(ptr > 0)

printf("buffer 2is greater than buffer 1n");

if(ptr < 0)

printf("buffer 2is less than buffer 1n");

if(ptr == 0)

printf("buffer 2equals buffer 1n");

return0;

}





6函数名:strcspn

功能:顺序在字符串s1中搜寻与s2中字符的第一个相同字符,返回这个字符在S1中第一次出现的位置。

说明:(返回第一个出现的字符在s1中的下标值,亦即在s1中出现而s2中没有出现的子串的长度。)

简单地说,若strcspn()返回的数值为n,则代表字符串s1开头连续有n个字符都不含字符串s2内的字符

:int strcspn(char str1, char str2);

程序例:

#include

#include

intmain(void)

{

char string1 = "1234567890";

char
string2 = "747DC8";

int length;

length= strcspn(string1, string2);

printf("Character wherestrings intersect is at position %dn", length);

return0;

}

7函数名:strdup

功能
:将串拷贝到新建的位置处

用法:char strdup(char str);

程序例:

#include

#include

intmain(void)

{

char dup_str, string = "abcde";

dup_str= strdup(string);

printf("%sn", dup_str);

free(dup_str);//
这是C语言C++用的是delete(dup_str);

return0;

}

8函数名:stricmp

功能
:以大小写不敏感方式比较两个串

用法:int stricmp(char str1, char str2);

程序例:

#include

#include

intmain(void)

{

char buf1 = "BBB", buf2 = "bbb";

int ptr;

ptr= stricmp(buf2, buf1);

if(ptr > 0)

printf("buffer 2 is greater than buffer1n");

if(ptr < 0)

printf("buffer 2 is less than buffer1n");

if(ptr == 0)

printf("buffer 2 equals buffer 1n");

return0;

}

9函数名:strerror

功能
:返回指向错误信息字符串的指针

用法:char *strerror(int errnum);

程序例:

#include

#include

intmain(void)

{

char *buffer;

buffer = strerror(errno);

printf("Error: %sn",buffer);

return 0;

}





10函数名:strncmp

能:比较字符串的前maxlen个字符

形式:intstrncmp(char str1, char str2, int maxlen)

程序例
:

#include

#include

intmain(void)

{

char buf1 = "aaabbb", buf2 = "bbbccc",*buf3 = "ccc";

int ptr;

ptr= strncmp(buf2,buf1,3);

if (ptr > 0)

printf("buffer 2 is greaterthan buffer 1n");

else

printf("buffer 2 is less than buffer 1n");

ptr= strncmp(buf2,buf3,3);

if (ptr > 0)

printf("buffer 2 is greaterthan buffer 3n");

else

printf("buffer 2 is less than buffer 3n");

return(0);

}





11函数名:strncpy

:串拷贝,将第二个字符maxlen个字符拷贝给第一个字符串


:char strncpy(char destin, char *source, int maxlen);

程序例
:

#include

#include

intmain(void)

{

char string[10];

char *str1 = "abcdefghi";

strncpy(string,str1, 3); //只复制三个字符

string[3] = '';

printf("%sn", string);


return 0;

}





12函数名:strpbrk

功能:依次检验字符串s1中的字符,当被检验字符在字符串s2中也包含时,则停止检验,并返回该字符位置,空字符NULL不包括在内。

说明:返回s1中第一个满足条件的字符的指针,如果没有匹配字符则返回空指针NULL

:char strpbrk(char str1, char *str2);

程序例:

#include

#include

intmain(void)

{

char string1 ="abcdefghijklmnopqrstuvwxyz";

char
string2 = "onm";

char *ptr;

ptr= strpbrk(string1, string2);

if(ptr)

printf("strpbrk foundfirst character: %cn", *ptr);

else

printf("strpbrk didn't find character in setn");

return0;

}









13函数名:strrchr

功能
:在串中查找指定字符的最后一个出现

用法:char strrchr(char str, char c);

程序例:

#include

#include

intmain(void)

{

char string[15];

char *ptr, c = 'r';

strcpy(string,"This is a string");

ptr = strrchr(string, c);

if (ptr)

printf("The character %c is at position: %dn", c,ptr-string);

else

printf("The character was not foundn");

return 0;

}





14函数名:strrev

:串倒转VC支持)


用法:char strrev(char str);

程序例:

#include

#include

intmain(void)

{

char *forward = "string";

printf("Beforestrrev(): %sn", forward);

strrev(forward);

printf("After strrev(): %sn", forward);

return 0;

}







15函数名:strset

功能:将一个串中的所有字符都设为指定字符VC支持)


用法:char strset(char str, char c);

程序例:

#include

#include

intmain(void)

{

char string[10] = "123456789";

char symbol = 'c';

printf("Beforestrset(): %sn", string);

strset(string,symbol);

printf("After strset(): %sn",string);

return 0;

}







16函数名:strtod

功能
:将字符串转换为double型值

用法:double strtod(char *str, char endptr);

程序例:**

#include

#include

intmain(void)

{

char input[80], *endptr;

double value;

printf("Entera floating point number:");

gets(input);

value = strtod(input, &endptr);

printf("Thestring is %s the number is %lfn", input, value);

return 0;

}

17函数名:strtok

功能
:查找由在第二个串中指定的分界符分隔开的单词

用法:char strtok(char str1, char *str2);

程序例:

#include

#include

intmain(void)

{

char input[16] = "abc,d";

char *p;

/strtok places a NULL terminator

in front of thetoken, if found
/

p = strtok(input, ",");

if (p) printf("%sn", p);

/A second call to strtok using a NULL

as the firstparameter returns a pointer

to the characterfollowing the token
/

p = strtok(NULL, ",");

if (p) printf("%sn", p);

return 0;

}







18函数名:strupr

功能:将串中的小写字母转换为大写字母


用法:char strupr(char str);

程序例:

#include

#include

intmain(void)

{

char string ="abcdefghijklmnopqrstuvwxyz", ptr;

/converts string to upper case characters /

ptr =strupr(string);

printf("%sn", ptr);

return 0;

}

19函数名:swab

功能
:交换字节

用法:void swab (char from, char to, int nbytes);

程序例:

#include

#include

#include

charsource[15] = "rFna koBlrna d";

char target[15];

intmain(void)

{

swab(source, target,strlen(source));

printf("This is target: %sn",target);

return 0;

原型:externchar strstr(char haystack, char *needle);

string重点函数集合
所在头文件:#include

string重点函数集合
功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)

string重点函数集合
说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL






原文链接: https://www.cnblogs.com/c-source/archive/2012/06/15/2564736.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/52786

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午4:12
下一篇 2023年2月9日 上午4:13

相关推荐