自己实现了VString中的一些功能,用以更好地理解字符串操作。希望可以互相交流。
#include <iostream>
#include <malloc.h>
#include <stdarg.h>
class MyString {
public:
MyString(char* s)
{
len = getLength(s)+1;
str = getOneMallocString(s, len);
}
MyString()
{
}
void destory()
{
free(str);
}
void setText(char* s)
{
if (str != NULL)
free(str);
len = getLength(s) + 1;
str = getOneMallocString(s, len);
}
char* getText()
{
return str;
}
int getLength()
{
int num = 0;
while (*(str+num) != '\0')
{
num++;
}
return num;
}
int getLength(char* s)
{
int num = 0;
while (*(s + num) != '\0') {
num++;
}
return num;
}
int getLength(const char* s)
{
int num = 0;
while (*(s + num) != '\0') {
num++;
}
return num;
}
bool isEmpty()
{
if (getLength() == 0)
return true;
else
return false;
}
MyString left(int nCount)
{
MyString m = MyString();
char* s = (char*)malloc((nCount + 1) * sizeof(char));
int num = 0;
while ( *(str + num) != '\0' && (num < nCount))
{
*(s + num) = *(str + num);
num++;
}
*(s + num) = '\0';
m.setText(s);
free(s);
return m;
}
/*loadString函数待开发*/
/*大写转变为小写*/
void makeLower()
{
int num = 0;
while (*(str + num) != '\0')
{
if (*(str + num) >= 'A' && *(str + num) <= 'Z')
*(str + num) += 32;
num++;
}
}
/*小写转变为大写*/
void makeUpper()
{
int num = 0;
while (*(str + num) != '\0')
{
if (*(str + num) >= 'a' && *(str + num) <= 'z')
*(str + num) -= 32;
num++;
}
}
/*字符倒置*/
void makeReverse()
{
int length = 0, i;
char temp;
length = getLength();
for (i = 0; i < length/2; i++) {
temp = *(str + i);
*(str + i) = *(str + length - 1 - i);
*(str + length - 1 - i)