使用String.h制作简单的String类
Making simple String class using string.h
我必须使用string.h
构建一个String类(当您看到代码时就会明白)。请原谅我英语不好。我不知道问题出在哪里。它只是崩溃了,没有任何错误消息。
(String.h
)
#ifndef _STRING_H
#define _STRING_H
class String{
public:
char* s1;
char* s2;
char* stemp;
//The Constructors
String();
String(char* so);
String(char* so, char* st);
//Member Functions
int slength(char* s1); //Calculate the length of Sting
void scopy(char* so,const char* st); // copy string value to another String
};
#endif // _STRING_H
(String.cpp
)
#include <string.h>
#include "String.h"
//The Constructors
String::String(){}
String::String(char* so){
s1 = so;
}
String::String(char* so, char* st){
s1 = so;
s2 = st;
}
//Member Functions
int String::slength(char* so){
return strlen(so);
}
void String::scopy(char* so,const char* st){
strcpy(so,st);
}
(main.cpp
)
#include <iostream>
#include "String.h"
using namespace std;
int main()
{
String str("Hello","World");
cout<<"The First String is : "<<str.s1<<endl;
cout<<"The Second String is : "<<str.s2<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"The First String contains "<<str.slength(str.s1)<<" Letters"<<endl;
cout<<"The Second String contains "<<str.slength(str.s2)<<" Letters"<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"Copying The Second String in the First String . . ."<<endl;
str.scopy(str.s1,str.s2);
cout<<"The First String is : "<<str.s1<<endl;
cout<<"The Second String is : "<<str.s2<<endl;
return 0;
}
将字符串文本分配给str.s1
,并作为String::scopy()
中strcpy
的第一个参数传递。这非常糟糕,因为不允许修改字符串文字。
我刚刚解决了问题
(String.h)
#ifndef STRING_H
#define STRING_H
class String{
public:
//Data Members
char* s1;
int len ;
//The Constructors
String();
String(char* so);
//Member Functions
int slength(String &s1); //Calculate the length of Sting
void scopy(String &so,String &st); // copy string value to another String
int scompare(String &so, String &st); // compare two string and returns which one is bigger
void scombine(String &so, String &st); // combine two strings into one string
//The Destructor
~String();
};
#endif // STRING_H
(String.cpp)
#include <string.h>
#include "String.h"
//The Constructors
String::String(){
len = 0;
s1 = new char[len + 1];
}
String::String(char* so){
len = strlen(so);
s1 = new char[len + 1];
strcpy(s1, so);
}
//Member Functions
int String::slength(String &so){
return strlen(so.s1);
}
void String::scopy(String &so,String &st){
strcpy(so.s1,st.s1);
}
int String::scompare(String &so, String &st){
return strcmp(so.s1,st.s1);
}
void String::scombine(String &so, String &st){
len = so.len + st.len;
delete s1;
s1 = new char[len + 1];
strcpy(s1,so.s1);
strcat(s1,st.s1);
}
//The Destructor
String::~String(){}
(main.cpp)
#include <iostream>
#include "String.h"
using namespace std;
int main()
{
String str1("Hello");
String str2("World");
String str;
cout<<endl<<"The First String is : "<<str1.s1<<endl;
cout<<"The Second String is : "<<str2.s1<<endl;
cout<<endl<<"-------------------------------------"<<endl;
cout<<endl<<"The First String contains "<<str.slength(str1)<<" Letters"<<endl;
cout<<"The Second String contains "<<str.slength(str2)<<" Letters"<<endl;
cout<<endl<<"-------------------------------------"<<endl;
cout<<endl<<"After Comparing the Two Strings we Find out That"<<endl;
switch(str.scompare(str1,str2)){
case 1:
cout<<"The First String is Bigger than the Second String";
break;
case 0:
cout<<"The two Strings are the equal";
break;
default:
cout<<"The Second String is Bigger than the First String";
}
cout<<endl<<endl<<"-------------------------------------"<<endl;
cout<<endl<<"After Combining The two Strings"<<endl;
str.scombine(str1,str2);
cout<<str.s1<<endl;
cout<<endl<<"-------------------------------------"<<endl;
cout<<endl<<"After copying The Second String in the First String . . ."<<endl;
str.scopy(str1,str2);
cout<<"The First String is : "<<str1.s1<<endl;
cout<<"The Second String is : "<<str2.s1<<endl;
return 0;
}
相关文章:
- 在c++中用vector填充一个简单的动态数组
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 使用简单类型列表实现的指数编译时间.为什么
- cppcheck在const std::string[]上引发警告
- 如何在BST的这个简单递归实现中消除警告
- 将std::string传递给WriteConsole API
- 一种在C++中读取TXT配置文件的简单方法
- 为std::string的某个索引赋值
- 关于简单C++函数(is_palindrome)的逻辑的问题
- std中有类似find_last_of的函数,而string中没有
- GDB 8.1 无法在单线程简单程序中跟踪 std::string 变量的值
- 为什么一个简单的bimap(std::string <-> int)不起作用?
- 将 ASCII 文件读取到C++ std::string 中的最简单方法是什么?
- 错误:无法在简单的 std::string 文件中转换
- 如何设计一个简单的 std::string-to-boost::p osix_time::p time 解析库
- 使用String.h制作简单的String类
- 在c++中简单地将System::String转换为std::String
- 带有自定义类的范围循环(矢量的简单版本<string>,使用分配器)
- 简单的问题:为什么字符串库函数std::string::find总是返回std::string::npos