char类型函数(C++入门?)

前几天测试,本来想用char自带的函数
然而发现自己并不是很会。。。

1.0 复制

1.1 strcpy

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char src[]="abcde";
    char dest[100];
    strcpy(dest,src);
    printf("%s",dest);
    //输出>> abcde 
}

1.2 strncpy(部分复制)

原型: char* strncpy(char* dest, char* src, int size_tn);
功能:将字符串src中最多n个字符复制到字符数组dest中(ta并不像strcpy一样遇到NULL才停止复制,而是等凑够n个字符才开始复制),返回指向dest的指针
说明:如果n > dest串长度,dest栈空间溢出产生崩溃异常

  • src串长度<=dest串长度
    如果n=(0, src串长度),src的前n个字符复制到dest中
    但是由于没有NULL字符,所以直接访问dest串会发生栈溢出的异常情况
    这时,一般建议采取memset将dest的全部元素用null填充
    • 如果n = src串长度,与strcpy一致
    • 如果n = dest串长度,[0,src串长度]处存放于desk字串,(src串长度, dest串长度]处存放NULL
  • src串长度>dest串长度
    • 如果n =dest串长度,则dest串没有NULL字符,会导致输出会有乱码
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char src[]="SWT is so great! We should % him everyday.";
    char dest[100];

    memset(dest,0,sizeof(dest));
    strncpy(dest,src,10);
    printf("%s\n",dest);
    //输出>>SWT is so 

    memset(dest,0,sizeof(dest));
    strncpy(dest,src,sizeof(src));
    printf("%s\n",dest);
    //输出>>SWT is so great! We should % him everyday.

    memset(dest,0,sizeof(dest));
    strncpy(dest,src,sizeof(dest));
    printf("%s\n",dest);
    //输出>>SWT is so great! We should % him everyday.

    char des[10];
    memset(des,0,sizeof(des));
    strncpy(des,src,sizeof(src));
    printf("%s\n",des);
    //exe停止工作 
}

2.0 合并

2.1 strcat

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char src[]="OI!";
    char dest[]="We like ";
    strcat(dest,src);
    printf("%s",dest);
    //输出>>We like OI! 
}

2.2 strncat(部分合并)

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char src[]="Please login in here!#$%@$@%@#$@%";
    char dest[]="Welcome to the largest talking room of SLYZ! ";
    strncat(dest,src,21);
    printf("%s",dest);
    //输出>>Welcome to the largest talking room of SLYZ! Please login in here! 
}

3.0 查找

3.1 strchr(查找字符)

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char src[]="Can you find some thing?";
    int t=strchr(src,'?')-src;
    printf("%d",t);
    //输出>>23
} 

3.2 strchr(查找字符串)

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char src[]="Can you find some thing?";
    int t=strstr(src,"thing")-src;
    printf("%d",t);
    //输出>>18
}

4.0 比较

4.1 strcmp(区分大小写)

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char src[]="Hello!";
    char dest[]="hello!";
    if (!strcmp(src,dest)) printf("SAME");
    else printf("DIFFERENT");
    //输出>>DIFFERENT 
}

4.2 stricmp(不区分字母的大小写)

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char src[]="Hello!";
    char dest[]="hello!";
    if (!stricmp(src,dest)) printf("SAME");
    else printf("DIFFERENT");
    //输出>>SAME
}

4.3 strncmp (部分比较,区分字母的大小写)

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char src[]="A APPLE A DAY.";
    char dest[]="a apple a day.";
    if (strncmp(src,dest,8)==0) printf("SAME");
    else printf("DIFFERENT");
    //输出>>DIFFERENT 
} 

4.4 strnicmp (部分比较,不区分字母的大小写)

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char src[]="A APPLE A DAY.";
    char dest[]="a apple a day.";
    if (strnicmp(src,dest,8)==0) printf("SAME");
    else printf("DIFFERENT");
    //输出>>SAME 
} 

5.0 转化

5.1 strupr(小写转大写)

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char s[]="Let's % SWT together!";
    strupr(s);
    printf("%s",s);
    //输出>>LET'S % SWT TOGETHER!
}

5.1 strlwr(大写转小写)

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char s[]="Let's % SWT together!";
    strlwr(s);
    printf("%s",s);
    //输出>>let's % swt together!
}

5.3 strrev(倒置)

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char s[]="!uoy kcor lliw eW";
    strrev(s);
    printf("%s",s);
    //输出>>We will rock you!
}

5.4 strset(设置)

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

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
    char s[]="(!@&*#$^*@#&^(!@#*))";
    strset(s,'%');
    printf("%s",s);
    //输出>>%%%%%%%%%%%%%%%%%%%%
}
发布了941 篇原创文章 · 获赞 192 · 访问量 32万+

猜你喜欢

转载自blog.csdn.net/wu_tongtong/article/details/79004668