#include <iostream>
using namespace std;
int strlength(char *str)
{
int length = 0;
int index = 0;
while (*(str + index)) {
length++;
index++;
}
return length;
}
char *mergeSortedStrings(char str1[], char str2[])
{ //get the length of the newstring
int str1len = strlength(str1);
int str2len = strlength(str2);
int newStringlen = str1len + str2len;
char *newString= new char[newStringlen];
int i, j = 0;
char temp;
// copy str2 to the end of str1
for (i = strlength(str1); i<256; i++)
{
str1[i] = str2[j];
j++;
}
for (i=0; i<newStringlen;i++)
for (j = newStringlen - 1; j > i; j--) {
if (str1[j - 1] > str1[j])
{
temp = str1[j];
str1[j] = str1[j - 1];
str1[j - 1] = temp;
}
}
str1[newStringlen] = '\0';
return str1;
}
void main()
{
char str1[20];
char str2[20];
cout << "Input string 1: ";
cin >> str1;
cout << "Input string 2: ";
cin >> str2;
cout << "The merged string is: " << mergeSortedStrings(str1, str2) << endl;
}
这是C ++字符串合并和排序编程,不使用字符串库函数。当我使用for循环将最终str1的内容复制到newString时,它没有给出正确的结果,因此我只使用了str1作为结果。它以某种方式给出了正确的结果,但我认为这可能效率不高。 你能帮帮我吗?
答案 0 :(得分:0)
这是一个如何使用自定义String类来完成它的示例。如果您不明白发生了什么,我建议您查看:NULL terminated string,copy constructor,operator overloading。 请注意,这只是一个示例,可以让您了解如何解决问题,可能会出现一些错误。
#include <iostream>
using namespace std;
class String
{
private:
char *_str;
int _len;
public:
String()
{
_str = NULL;
_len = 0;
}
~String()
{
if (_str != NULL)
delete _str;
}
String(const char *str)
{
_str = NULL;
_len = 0;
if (str == NULL) return;
int i;
for (i = 0; str[i] != '\0'; i++);
_len = i;
_str = new char[_len];
for (i = 0; i < _len; i++)
_str[i] = str[i];
}
String(const String &s)
{
_str = NULL;
_len = 0;
if (s._len == 0) return;
_str = new char[s._len];
_len = s._len;
for (int i = 0; i < _len; i++)
_str[i] = s._str[i];
}
//constructor for concatenation
String(const char* a, const char* b)
{
_str = NULL;
_len = 0;
if (a == NULL || b == NULL)
return;
int lenA;
for (lenA = 0; a[lenA] != '\0'; lenA++);
int lenB;
for (lenB = 0; b[lenB] != '\0'; lenB++);
_len = lenA + lenB;
_str = new char[_len];
for (int i = 0; i < lenA; i++)
_str[i] = a[i];
for (int i = 0; i < lenB; i++)
_str[lenA + i] = b[i];
}
int getLength() const { return _len; }
operator const char*() { return _str; }
void operator=(const char* str)
{
_str = NULL;
_len = 0;
if (str == NULL) return;
int i;
for (i = 0; str[i] != '\0'; i++);
_len = i;
if (_str != NULL)
delete _str;
_str = new char[_len];
for (i = 0; i < _len; i++)
_str[i] = str[i];
}
String operator+(const char* str)
{
return String(*this, str);
}
bool operator==(const char* str)
{
int len;
for (len = 0; str[len] != '\0'; len++);
if (len != _len) return false;
for (int i = 0; i < len; i++)
if (str[i] != _str[i])
return false;
return true;
}
bool operator<(const char* str)
{
int i;
for (i = 0; i < _len && str[i] != '\0'; i++)
{
if (_str[i] < str[i])
return true;
else if (_str[i] > str[i])
return false;
}
return false;
}
bool operator<=(const char* str)
{
int i;
for (i = 0; i < _len && str[i] != '\0'; i++)
{
if (_str[i] < str[i])
return true;
else if (_str[i] > str[i])
return false;
}
return (i == _len);
}
bool operator>(const char* str)
{
int i;
for (i = 0; i < _len && str[i] != '\0'; i++)
{
if (_str[i] > str[i])
return true;
else if (_str[i] < str[i])
return false;
}
return (_len > i);
}
bool operator>=(const char* str)
{
int i;
for (i = 0; i < _len && str[i] != '\0'; i++)
{
if (_str[i] > str[i])
return true;
else if (_str[i] < str[i])
return false;
}
return (_len >= i);
}
friend istream& operator>>(istream &is, String &s)
{
char str[512];
is.getline(str, 512);
s = str;
return is;
}
};
int main()
{
String str1;
String str2;
cout << "Input string 1: ";
cin >> str1;
cout << "Input string 2: ";
cin >> str2;
String sortedMerged = str1 <= str2 ? str1 + str2 : str2 + str1;
cout << sortedMerged;
return 0;
}