strncpy():复制字符串的子串
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s1[20] = "I love dog";
char s2[20];
strncpy(s2, s1 + 2, 2);
cout << s2 << endl;
return 0;
}
//lo
strstr()
- strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s1[20] = "I love dog";
char s2[20] = "love";
cout << "子字符串是:" << strstr(s1, s2) << endl;
return 0;
}
//子字符串是:love dog
strrev():反转字符串的函数
#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "hello";
strrev(s);
puts(s);
return 0;
}
//olleh
reverse()
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s = "hello";
reverse(s.begin(), s.end());
cout << s << endl;
return 0;
}
//olleh
注意:
1.strrev函数只对字符数组有效,对string类型是无效的。
2.reverse函数是反转容器中的内容,对字符数组无效。