您的位置:首页 > 编程语言 > C语言/C++

C++字符串相关操作函数

2016-05-06 17:25 525 查看
#include <IOSTREAM>
using namespace std;
/*字符串相关操作函数:strcat、strcpy、strcmp、strupr、strlwr、strlen*/
void main()
{
char str0[100] = "This is a C++ program!";
char str1[10] = "So what!";
//strcat 字符串连接函数
strcat(str0,str1);
cout<<"str0:";
puts(str0);

//strcpy 字符串复制函数
strcpy(str0,str1);
cout<<"str0:";
cout<<str0<<endl;
cout<<"str0[12]:"<<str0[12]<<endl;

//strcmp 字符串比较函数
char str2[] = "So whas!";
cout<<strcmp(str1,"So what!")<<endl;
cout<<strcmp(str1,str2)<<endl;
cout<<strcmp(str1,"So waat!adff")<<endl;
cout<<strcmp(str1,"So wkat!")<<endl;
cout<<strcmp(str1,"So what!adff")<<endl;

//strupr 字符串全部转化为大写
strupr(str2);
puts(str2);

//strlwr 字符串全部转化为小写
strlwr(str2);
puts(str2);

//strlen 字符串长度(不包括字符串结束符'\0')
cout<<"str2的长度:"<<strlen(str2)<<endl;
}
/*
str0:This is a C++ program!So what!
str0:So what!
str0[12]:+
0
1
1
-1
-1
SO WHAS!
so whas!
str2的长度:8
Press any key to continue
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: