1 串的基本用法
1.1 基操
char *s1 = "It is a car";
char s2[20] = "jeep";
char *s3 = "car";
char s4[20];
char *s5;
//[1] 字符串长度 int strlen(const char *str);
cout << "strlen(s1) : " << strlen(s1) << endl; //11
//[2] 串拷贝 char *strcpy(char *_Dest, const char *_Source);
strcpy(s4, s3);
cout << s4 << endl;
//[3] 串连接 char *strcat(char *_Dest,const char *_Source); _Source 添加到 _Dest后
s5 = strcat(s2, s3);
cout << s5 << endl;
//[4] 串比较 int strcmp(char *src1,char *src2);
int result;
result = strcmp(s2, s3); cout << result << endl; // 1
result = strcmp(s5, "jeepcar"); cout << result << endl; // 0
result = strcmp(s3, s2); cout << result << endl; // -1
//[5] 串定位 char *strchar(char *str1, char ch); 第一次出现的位置
char *ret;
ret = strchr(s1, 'a'); cout << ret << endl; // a car
1.2 静态存储
超过容量将被截断。
const int MAXSIZE = 10;
char str[MAXSIZE];
1.3 动态存储(堆分配存储 - 常用)
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
struct StrNode
{
char *str;
int size;
};
int main()
{
StrNode s1;
s1.str = new char[s1.size];
s1.str = "xhh 666";
cout << s1.str << endl;
cout << strlen(s1.str) << endl;
return 0;
}
2 字符串实现
class String
{
private:
char *str;
int size;
public:
String(); //默认构造
String(char *chx); //带参构造
~String(); //析构函数
void create();
void display();
int length(){return this->size;} //字符串长度
String subString(int pos, int num); //字符串运算
void insert(String s, int pos); //插入
void del(int pos, int num); //删除
String operator +(String s); //+重载
int findStr(String T, int pos); //查找子串
};
String::String()
{
this->str = new char;
if(!str) {cout << "__ error !" << endl; return;}
this->size = 0;
str[0] = '\0';
}
String::String(char *chx)
{
this->size = strlen(chx); //长度
this->str = new char[this->size];//动态分配
strcpy(this->str, chx); //赋值
}
String::~String()
{
delete[] this->str;
}
void String::create()
{
char strIn[30];
cout << "Input your str : "; cin >> strIn;
this->size = strlen(strIn);
strcpy(this->str, strIn);
}
void String::display()
{
cout << "this->str : " << this->str << endl;
cout << "this->len : " << this->size << endl;
}
String String::subString(int pos, int num)
{
int idx = pos - 1; //索引
String tp; //空串
//如果索引越界 || num小于等于0 则返回空串
if(idx < 0 || idx > this->size-1 || num <= 0) return tp;
// [xx_0, xx_1, xx_2, xx_3] pos = 3
// size = 4; idx = 2; left = size - idx = (2)
int leftNum = this->size - idx; //pos开始剩余的个数
if(num > leftNum) num = leftNum;
delete[] tp.str; tp.str = new char[num+1]; //重新申请存放字符串的空间
for(int i = 0, j = idx; i < num; i++, j++) tp.str[i] = this->str[j];
tp.str[num] = '\0'; //字符串结束标志
tp.size = num; //字符串的长度
return tp;
}
void String::insert(String s, int pos)
{
int idx = pos-1;
char temp[30]; //临时存储
if(idx < 0 || idx > this->length()-1) {cout << "pos is error." << endl;}
else
{
int newSize = this->size + s.size;
strcpy(temp, this->str); //中间变量
//重新分配空间
delete[] this->str;
this->str = new char[newSize + 1];
strcpy(this->str, temp); //恢复主串
for(int i = idx; i < idx + s.size; i++) this->str[i + s.size] = str[i]; //向后平移s.size 个元素
for(int i = idx, j = 0; i < idx + s.size; i++, j++) this->str[i] = s.str[j]; //赋值s.size 个元素
this->size = newSize; //新串的长度
this->str[newSize] = '\0'; //结束标志
}
}
void String::del(int pos, int num)
{
int idx = pos - 1;
if(idx < 0 || idx > this->length()-1) {cout << "pos is error." << endl;}
else
{
int leftNum = this->size - idx; //pos开始剩余的个数
if(num > leftNum)//截尾
{
this->str[idx] = '\0';
this->size = idx - 1;
}
else //移动元素
{
for(int i = idx, j = idx + num; j < this->length(); i++, j++)
{
this->str[i] = this->str[j];
}
this->size = this->size - num;
this->str[this->size] = '\0';
}
}
}
// for test
cout << "----------- [1] -----------" << endl;
String s1("xhh 0608");
s1.display();
String ret = s1.subString(5, 4);
ret.display();
cout << "----------- [2] -----------" << endl;
String s2("zph_kkk");
s1.insert(s2, 4);
s1.display();
cout << "----------- [3] -----------" << endl;
s1.del(5, 2);
s1.display();
cout << "----------- [4] -----------" << endl;
s1.del(10, 6);
s1.display();