字符串函数的总结

来源:互联网 发布:网络延长器是什么 编辑:程序博客网 时间:2024/05/01 15:06

1.strrev(char *a)

表示把一个字符串a倒叙。
例如:

#include<bits/stdc++.h>using namespace std;char a[]="string";int main(){    strrev(a);    cout<<a<<endl;}

2.strcmp(char *a,const char *b)&strncmp(char *a,const char *b,int size)

前者是把整个字符串b复制到a中,而后者是把b中size个元素复制到a中

#include<bits/stdc++.h>using namespace std;char b[500]="Coder_LL";int main(){    strcpy(b,"strcpy");    cout<<b<<endl;     //you can see the whole string has been changed;    strncpy(b,"123456",3);    cout<<b;           //you can see only "str" has been changed into "123" but the "cpy" hasn't;}

strcasecmp(char *a,char *b)和strncpy(char *a,char *b,int size)

第一个函数指忽略大小写比较字典序。
前者大返回正数,后者大返回负数,相等返回0。(类似于strcmp)。
第二个函数指从当前位置开始比较元素个数为size的字符串字典序大小。

#include<bits/stdc++.h>using namespace std;char b[500]="Coder_LL";int main(){    cout<<strcasecmp(b,"coder_ll")<<endl;    cout<<strcmp(b,"Coder_ll");    cout<<endl<<strncmp(b,"Co     ",2);}

strcat(char *a,char *b)和strncat(char *a,char *b,int size)

表示把b(或者前size个b)粘在a后面。

#include<bits/stdc++.h>using namespace std;char b[500]="Coder_LL";int main(){    strcat(b,"Sir");    cout<<b<<endl;    strncat(b,"Sir~_~",3);    cout<<b;}

strchr(char *a,char b)和strrchr(char *a,char b)

返回正序/逆序查找到的第一个在a中的b的位置开始的字符串

#include<bits/stdc++.h>using namespace std;char b[500]="Coder_LLer";int main(){    cout<<strchr(b,'e')<<endl;    cout<<strrchr(b,'e');;}

atof(char *n)和atoi(char *n)

前者返回寻找到的从第一个字符开始的一个连续小数–》float型
后者返回寻找到的从第一个字符开始的一个连续整数–》int型
注意:前者返回值是一个六位有效数字

#include<bits/stdc++.h>using namespace std;char b[]="-1000000.211419ds2";int main(){    cout<<atof(b)<<endl<<atoi(b);}

怎么感觉到了和背古文一样的痛苦^_^

原创粉丝点击