C++基础---char型字符串库函数

来源:互联网 发布:东莞网络优化推广公司 编辑:程序博客网 时间:2024/05/29 14:55

1. char型字符串库函数

1.1 赋值

  • 赋值: strcpy
  • 原型: char* strcpy(char* dest, char* src);
  • 功能: 把从src地址开始且含有 ‘\0’结束符的字符串复制到以dest开始的地址空间。返回指向dest的指针。
  • 说明: src 和 dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char src[]="My name is Mini.";     char dest[128];//定义数组长度要足够大    strcpy(dest, src);    cout<<"源字符串:"<<src<<endl;    cout<<"目标字符串:"<<dest<<endl;    system("pause");    return 0;}=>源字符串:My name is Mini.  目标字符串:My name is Mini.

1.2 替换

  • 替换: strncpy
  • 原型: char* strncpy(char* dest, char* src, int size_tn);
  • 功能:将字符串src中最多n个字符复制到字符数组dest中(它并不像strcpy一样遇到NULL才停止复制,而是等凑够n个字符才开始复制),返回指向dest的指针。
  • 说明:如果n > dest串长度,dest栈空间溢出产生崩溃异常。
    a)src串长度<=dest串长度,(这里的串长度包含串尾NULL字符)
    如果n=(0, src串长度),src的前n个字符复制到dest中。但是由于没有NULL字符,所以直接访问dest串会发生栈溢出的异常情况。这时,一般建议采取memset将dest的全部元素用null填充,如:memset(dest,0,7)(7为从dest起始地址开始前7个位置填充null,dest可以为字符指针和数组名)。注意:char* pc=”abc”; char chs[5]; sizeof(pc)为4(包含null)(有些编译器不行),sizeof(chs)为5。
    如果n = src串长度,与strcpy一致。
    如果n = dest串长度,[0,src串长度]处存放于desk字串,(src串长度, dest串长度]处存放NULL。
    b)src串长度>dest串长度
    如果n =dest串长度,则dest串没有NULL字符,会导致输出会有乱码。
    注:如果不考虑src串复制完整性,可以将dest最后一字符置为NULL。所以,一般把n设为dest(含null)的长度(除非将多个src复制到dest中)。当n=dest串长度时,定义dest为字符数组,因为这时没有null字符拷贝。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char src[]="My name is Mini.";     char dest[7];//定义数组长度要足够大    strncpy(dest, src,sizeof(dest));//n =dest串长度,导致出现乱码    cout<<"源字符串:"<<src<<endl;    cout<<"目标字符串:"<<dest<<endl;    system("pause");    return 0;}=>源字符串:My name is Mini.  目标字符串:My name烫烫烫烫蘉y name is Mini.

1.3 合并

  • 合并: strcat
  • 原型: char* strcat(char* dest, char* src);
  • 功能:把src所指字符串添加到dest结尾处(覆盖dest结尾处的 ‘\0’)并添加 ‘\0’。返回指向dest的指针。
  • 说明: src 和 dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char src[]="is Mini.";     char dest[128] ="My name "; //定义数组长度要足够大    strcat(dest, src);    cout<<"源字符串:"<<src<<endl;    cout<<"目标字符串:"<<dest<<endl;    system("pause");    return 0;}=>源字符串:is Mini.  目标字符串:My name is Mini.

1.4 部分合并

  • 部分合并: strncat
  • 原型: char* strncat(char* dest, char* src, int n);
  • 功能:把src所指字符串的前n个字符添加到dest结尾处(覆盖dest结尾处的 ‘\0’)并添加 ‘\0’。返回指向dest的指针。
  • 说明: src 和 dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char src[]="is Mini.";     char dest[128] ="My name ";//定义数组长度要足够大    strncat(dest, src, 2);    cout<<"源字符串:"<<src<<endl;    cout<<"目标字符串:"<<dest<<endl;    system("pause");    return 0;}=>源字符串:is Mini.  目标字符串:My name is

1.5 查找字符

  • 查找字符: strchr
  • 原型: char* strchr(char* s, char c);
  • 功能: 查找字符串s中首次出现字符c的位置。
  • 说明: 返回首次出现c的位置的指针,如果s中不存在c则返回NULL。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char src[]="My name is Mini.";     char dest='i';    char *p = strchr(src, dest);    if(NULL == p)    {        cout<<"在字符串中没找到字符"<<endl;    }    else    {        cout<<"在字符串中找到了:"<<*p<<endl;    }    system("pause");    return 0;}=>在字符串中找到了:i

1.6 查找字符串

  • 查找串: strchr
  • 原型: char* strstr(char* haystack, char* needle);
  • 功能: 从字符串haystack中寻找needle第一次出现的位置(不比较结束符”\0”)。
  • 说明: 返回指向第一次出现needle位置的指针,如果没找到则返回NULL。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char s1[] = "My name is Mini.";    char dest[]="no";//查找字符串    char *p = strstr(s1, dest);     if(p != NULL)    {        cout<<"查找字符串中某字符串第一次出现的位置:"<<*p<<endl;    }    else    {        cout<<"没有找到制定的字符串"<<endl;    }    system("pause");    return 0;}=>没有找到制定的字符串

1.7 比较,区分字母的大小写

  • 比较: strcmp
  • 原型: int strcmp(char* s1, char* s2);
  • 功能: 比较字符串s1和s2,区分大小写
  • 说明: 当s1 < s2时,返回值<0;
        当s1 = s2时,返回值=0;
        当s1 > s2时,返回值>0。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char s1[] = "MY NAME IS MINI.";    char s2[] = "my name is mini.";    int r= strcmp(s1, s2); //比较字符串s1和s2,区分字母的大小写    if(r == 0)    {        cout<<"两个字符串相等"<<endl;    }    else    {        if(r > 0)        {            cout<<"字符串s1大于字符串s2"<<endl;        }        else        {            cout<<"字符串s1小于字符串s2"<<endl;        }    }    system("pause");    return 0;}=>字符串s1小于字符串s2

1.8 比较,不区分字母的大小写

  • 比较: stricmp
  • 原型: int stricmp(char* s1, char* s2);
  • 功能: 比较字符串s1和s2,但不区分字母的大小写。
  • 说明: 当s1 < s2时,返回值<0;
        当s1 = s2时,返回值=0;
        当s1 > s2时,返回值>0。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char s1[] = "MY NAME IS MINI.";    char s2[] = "my name is mini.";    int r= stricmp(s1, s2);//比较字符串s1和s2,但不区分字母的大小写    if(r == 0)    {        cout<<"两个字符串相等"<<endl;    }    else    {        if(r > 0)        {            cout<<"字符串s1大于字符串s2"<<endl;        }        else        {            cout<<"字符串s1小于字符串s2"<<endl;        }    }    system("pause");    return 0;}=>两个字符串相等

1.9 部分比较,区分字母的大小写

  • 部分比较: strncmp
  • 原型: int strncmp(char* s1, char* s2, int n);
  • 功能: 比较字符串s1和s2的前n个字符。
  • 说明: 当s1 < s2时,返回值<0;
        当s1 = s2时,返回值=0;
        当s1 > s2时,返回值>0。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char s1[] = "MY NAME IS MINI.";    char s2[] = "my name is mini.";    int r= strncmp(s1, s2, 2);//比较字符串s1和s2的前2个字符,区分字母的大小写    if(r == 0)    {        cout<<"两个字符串相等"<<endl;    }    else    {        if(r > 0)        {            cout<<"字符串s1大于字符串s2"<<endl;        }        else        {            cout<<"字符串s1小于字符串s2"<<endl;        }    }    system("pause");    return 0;}=>字符串s1小于字符串s2

1.10 部分比较,不区分字母的大小写

  • 部分比较: strnicmp
  • 原型: int strnicmp(char* s1, char* s2, int n);
  • 功能: 比较字符串s1和s2的前n个字符但不区分大小写。
  • 说明: 当s1 < s2时,返回值<0;
        当s1 = s2时,返回值=0;
        当s1 > s2时,返回值>0。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char s1[] = "MY NAME IS MINI.";    char s2[] = "my name is mini.";    int r= strnicmp(s1, s2, 2);//比较字符串s1和s2的前2个字符,但不区分字母的大小写    if(r == 0)    {        cout<<"两个字符串相等"<<endl;    }    else    {        if(r > 0)        {            cout<<"字符串s1大于字符串s2"<<endl;        }        else        {            cout<<"字符串s1小于字符串s2"<<endl;        }    }    system("pause");    return 0;}=>两个字符串相等

1.11 小写转大写

  • 小写->大写: strupr
  • 原型: char* strupr(char* s);
  • 功能: 将字符串s转换为大写形式。
  • 说明: 只转换s中出现的小写字母,不改变其它字符。返回指向s的指针。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char s1[] = "My name is Mini.";    strupr(s1);    cout<<"字符串中的小写字母转为大写字母后:"<<s1<<endl;    system("pause");    return 0;}=>字符串中的小写字母转为大写字母后:MY NAME IS MINI.

1.12 大写转小写

  • 大写->小写: strlwr
  • 原型: char* strlwr(char* s);
  • 功能: 将字符串s转换为小写形式。
  • 说明: 只转换s中出现的大写字母,不改变其它字符。返回指向s的指针。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char s1[] = "My name is Mini.";    strlwr(s1);    cout<<"字符串中的大写字母转为小写字母后:"<<s1<<endl;    system("pause");    return 0;}=>字符串中的大写字母转为小写字母后:my name is mini.

1.13 获取字符串长度

  • 获取字符串长度: strlen
  • 原型: int strlen(char* s);
  • 功能: 计算字符串s的可见字符实际长度。
  • 说明: 返回s的长度,不包括结束符“\0”。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char s1[] = "My name is Mini.";    int len = strlen(s1);    cout<<"字符串中的可见字符长度:"<<len<<endl;    system("pause");    return 0;}=>字符串中的可见字符长度:16

1.14 倒置

  • 倒置: strrev
  • 原型: char* strrev(char* s);
  • 功能: 把字符串s的所有字符的顺序颠倒过来(不包括空字符”\0”)。
  • 说明: 返回指向颠倒顺序后的字符串指针。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char s1[] = "abcdefghijklmnopqrstuvwxyz";    strrev(s1);    cout<<"字符串导致后:"<<s1<<endl;    system("pause");    return 0;}=>字符串导致后:zyxwvutsrqponmlkjihgfedcba

1.15 设置

  • 设置: strset
  • 原型: char* strset(char* s, char c);
  • 功能: 把字符串s中的所有字符都设置成字符c。
  • 说明: 返回指向s的指针。
  • 代码示例:

    #include <iostream>using namespace std;int main(){    char s1[] = "My name is Mini.";    char dest='c';    strset(s1, dest);    cout<<"把字符串s中的所有字符都设置成字符c:"<<s1<<endl;    system("pause");    return 0;}=>字符串导致后:zyxwvutsrqponmlkjihgfedcba

参考文献:
[1] 百度搜索关键字:char型字符串库函数

0 0
原创粉丝点击