C++常用字符串函数用法整理
strlen(s1)
功能:求取字符串的长度
返回值:返回字符串s1的长度
#include <iostream>
using namespace std;
#include <cstring>
int main(int argc, char* argv[])
{
char s[20] = "hello";
int len = strlen(s);
cout << "sizeof(s) = " << len << endl; //输出s的长度
return 0;
}
strcpy(s1,s2)
功能:复制字符串 s2 到字符串 s1。
返回值:返回字符串 s1 的起始位置
说明:如果字符串 s1 的内存空间不够大,可能会造成缓冲溢出的情况,所以字符串 s1 的长度必须大于或等于字符串 s2 的长度。且复制完后其 s2 的内容将覆盖 s1 的内容。
#include <iostream>
using namespace std;
#include <cstring>
int main(int argc, char* argv[])
{
char s1[20] = "hello";
char s2[20] = "world";
strcpy(s1 ,s2);
cout << "strcpy(s1 ,s2) = " << s1 << endl; //输出为 s2 覆盖 s1 的内容
return 0;
}
strncpy(s1,s2,len)
功能:将字符数组 s2 前 len 个字符复制到数组 s1 的前 len个字符空间
说明:第二个参数可以是数组名,也可以是字符串,但是第三个参数必须为正整数。
#include <iostream>
using namespace std;
#include <cstring>
int main(int argc, char* argv[])
{
char s1[20] = "hello";
char s2[20] = "world";
strncpy(s1 ,s2,3);
cout << "strcpy(s1 ,s2,3) = " << s1 << endl; //输出为 s2 前三个字符覆盖s1前三个字符worlo
return 0;
}
strcat(s1,s2)
功能:连接字符串 s2 到字符串 s1 的末尾。
返回值:返回字符串 s1 的起始位置。
说明:该函数的第二个参数也可以为字符串常量
#include <iostream>
using namespace std;
#include <cstring>
int main(int argc, char* argv[])
{
char s1[20] = "hello";
char s2[20] = "world";
strcat(s1 ,s2);
cout << "strcat(s1 ,s2) = " << s1 << endl; //输出为s1 + s2字符串helloworld
return 0;
}
strcmp(s1,s2)
功能:用来比较 s1 和 s2 两个字符串。
返回值: 当 s1 和 s2 是相同时,则返回 0 ,当 s1 < s2, 则返回值小于 0 ,如果 s1 大于 s2,则返回大于 0;
说明:比较方式是按字符的ASCII 码。
#include <iostream>
using namespace std;
#include <cstring>
int main(int argc, char* argv[])
{
int s1 = strcmp("hello","hello");
int s2 = strcmp("Hello","hello");
int s3 = strcmp("hello","Hello");
cout << "s1等于s2时" << s1 << endl; //字符串一样,结果 0
cout << "s1小于s2时" << s2 << endl; //字符串hello的ascii大于 Hello结果-1
cout << "s1大于s2时" << s3 << endl; //结果 1
return 0;
}
strncmp(s1,s2,len)
功能:跟 strcmp一样,只不过是比较两个字符串中的前 len 个。
返回值: 当 s1 和 s2 前 len 是相同时,则返回 0 ,当 s1 < s2, 则返回值小于 0 ,如果 s1 大于 s2,则返回大于 0;
说明: len 为正整数
#include <iostream>
using namespace std;
#include <cstring>
int main(int argc, char* argv[])
{
int s1 = strcmp("hello","hello",3);
int s2 = strcmp("Hello","hello",3);
int s3 = strcmp("hello","Hello",3);
cout << "s1等于s2时" << s1 << endl; //字符串一样,结果 0
cout << "s1小于s2时" << s2 << endl; //字符串hel的ascii大于 Hel结果-1
cout << "s1大于s2时" << s3 << endl; //结果 1
return 0; //因为比较两个字符串中的前三个,所有结果还是一样
}
strlwr(s1)
功能:将字符串 s1 中的所有的大写字母转换为小写字母,其他的不变
#include <iostream>
using namespace std;
#include <cstring>
int main(int argc, char* argv[])
{
char s1[20] = "HeLLo WorlD";
cout << strlwr(s1) << endl; //输出结果为hello world
return 0;
}
strupr(s1)
功能: 用法和 strlwr 相反,是将小写字母转换为大写字母,其他不变
#include <iostream>
using namespace std;
#include <cstring>
int main(int argc, char* argv[])
{
char s1[20] = "HeLLo WorlD2";
cout << strupr(s1) << endl; //输出HELLO WORLD2
return 0;
}
strstr(s1,s2)
功能:用来定义判断 s2 是否为 s1 的字串
返回值:如果是,则返回 s2 首次在 s1 中首次出现的地址,如果否,则返回NULL
#include <iostream>
using namespace std;
#include <cstring>
int main(int argc,char* argv[])
{
char s1[20] = "qwertyuiop";
char s2[20] = "yuiop";
char s3[20] = "iods";
cout << strstr(s1,s2) << endl; //返回 s2 在 s1首次出现的地址 输出yuiop
cout << strstr(s1,s3) << endl; //返回NULL
return 0;
}
strrev(s1)
功能:实现字符反转
说明:只对字符数组有效,对 string 类型是无效的
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc,char*argv[])
{
char s1[20] = "hello";
cout << "反转前: " << s1 << endl;//输出hello
strrev(s1);
cout << "反转后:" << s1 << endl; //输出olleh
return 0;
}