当前位置:
首页 >
Str库系列函数合集(strlen、strcpy、strcmp、strcat、strchr等)
发布时间:2024/2/28
40
豆豆
生活随笔
收集整理的这篇文章主要介绍了
Str库系列函数合集(strlen、strcpy、strcmp、strcat、strchr等)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
关于Str函数,网上五花八门,使初学者很容易迷失方向,笔者在这里做一个常用函数的总结。希望对读者起到些许帮助。
后续会持续更新特殊函数~
想了解mem系列函数的,请猛戳这里→mem系列函数
代码
//头文件:#include<string.h> #include <iostream> #include <string.h> #include <string> #include <cstdio>using namespace std ;int main() {char a[10] = "123456789" ; //如此定义只能定义10-1个。最后一个放\0 ,最后一个需自己定义。 char b[10] = "qwertyuio" ;// 1、strlen (求长度) // cout << strlen(a1) ; // 2、strcpy 、strncpy (复制) // strcpy(a,b) ; // strncpy(a,b+3,3) ; 意思是将b字符串的第4-7个值赋给a字符串。// 3、strcmp 、strncmp (比较大小) // cout << strcmp(a,b) ; // cout << strncmp(a,b+3,3) ; 同上// 4、strcat 、 strncat (字符串连接) // strcat(a,b) ; // strncat(a,b+3,3) ; 同上// 5、strchr 、 strrchr (查找、反向查找)//一定、必须要用以下三行代码才能返回该字符的位置。 strrchr同理 // char *p ; // p = (char*)(strchr(a,'5')) ; // cout << "要查询的数在数组第" <<(int)(p-a+1) << "位。" << endl ; //6、特殊查找string a , b ;cin >> a >> b ; // 返回b串中第一个与a串不匹配字符的位置 // int found = a.find_first_not_of(b) ; // int found = a.find_first_not_of(b,1) ; //从b串的第二个开始找 // int found = a.find_first_not_of(b,1,4) ; //从b串的第二个开始找,到第5个结束 cout << found+1 ; //返回值需+1.// 返回b串中第一个与a串匹配字符的位置 // int found = a.find_first_of(b) ; return 0 ;} 超强干货来袭 云风专访:近40年码龄,通宵达旦的技术人生总结
以上是生活随笔为你收集整理的Str库系列函数合集(strlen、strcpy、strcmp、strcat、strchr等)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: UVa1588 | 算法竞赛入门经典(第
- 下一篇: mem库系列函数合集(memset、me