关于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 ;
}