#include<iostream>
using namespace std;
class String {
public:
//定义类的成员变量
char s[10] = "abcd";
char *ps = s;
const char t[10] = "dcba";
const char *pt = t;
char S[10] = "ABCDEF";
//构造函数部分
String();
String(const String & c);
//字符串处理函数
//连接
void strcat();
//复制
void strcpy();
//复制n个
void strcpy(int n);
//比较
int strcmp();
//取长度
int strlen( char *s);
//转大小写
void strwr( char *s);
void strup( char *s);
~String();
};
String::~String()
{
}
String::String()
{
}
//拷贝函数
String::String(const String & c) { //由于是拷贝,所以把参数用const限定
const char* s = c.s;
cout << "1";
};
void String::strcat()
{
while (*ps) { //把指针移动到最后
++ps;
}
while (*pt) { //当*pt有值 就进入
*ps = *pt; //复制
++pt; //指针后移
++ps;
}
cout << s << endl; //输出下连接复制是否正确
return;
}
void String::strcpy()
{
while ((*ps = *pt) != '\0') { //一直到*pt指向的内容为\0就跳出
ps++; //往后移动指针
pt++;
}
*ps = *pt; //把\0也给*ps
cout << s << endl;
return;
}
void String::strcpy(int n) //复制前n个
{
int i = 0;
while (*pt) { //首先也要判断pt有值
if (i<n) {
*ps = *pt;
++i;
ps++;
pt++;
}
else { //如果已经复制了n个 那就结束
break;
}
}
//结束复制之后要 最后加空字符
*ps = '\0';
cout << s << endl;
return;
}
int String::strcmp()
{
//用for循环 找到不相等的地方
for (; *ps == *pt; ++ps, ++pt) {
if (*ps == '\0') { //如果两个完全相等 会进去
return 0;
}
}
return (*ps - *pt);
//返回当前指的位置 相减 就是字符对应的数值相减
}
int String::strlen( char * s)
{
int i = 0;
while (*s) { //计算的有效长度(不加空字符)
++s;
++i;
}
return i;
}
void String::strwr( char * s)
{
char*m = s;
while (*s)
{ //要判断是否为大写字母
if (*s >= 'A'&&*s <= 'Z') {
*s += 32;
}
++s;
}
//由于指针已经把 类里面的成员S 变为小写
//故直接输出就可以验证
cout << S << endl;
return;
}
void String::strup( char * s)
{
char*m = s;
while (*s)
{
if (*s >= 'a'&&*s <= 'z') {
*s -= 32;
}
++s;
}
cout << S << endl;
return;
}
void main()
{
//调用函数
cout << "strcat:" << endl;
String e1;
e1.strcat();
cout << endl;
cout << "strcpy:" << endl;
String e2;
e2.strcpy();
cout << endl;
cout << "strncpy:" << endl;
String e3;
e3.strcpy(3);
cout << endl;
String e4;
cout << "strcmp:" << endl << e4.strcmp() << endl;
cout << endl;
String e5;
cout << "strlen:" << endl << e5.strlen(e5.s) << endl;
cout << endl;
cout << "strwr:" << endl;
String e6;
e6.strwr(e6.S);
cout << endl;
cout << "strup:" << endl;
String e7;
e7.strup(e6.S);
cout << endl;
system("pause");
return;
}