More actions
imported>enochbible No edit summary |
(Repair batch-0005 pages from live compare) |
||
| Line 120: | Line 120: | ||
// 길이 파악 | // 길이 파악 | ||
size = 0; | size = 0; | ||
for(;original | for(;original[size] != '\0'; size++); | ||
count = size; | count = size; | ||
// value에 데이터 복사 | // value에 데이터 복사 | ||
value = new char | value = new char[size]; | ||
for(int i=0; i < size; i++) | for(int i=0; i < size; i++) | ||
value | value[i] = original[i]; | ||
offset = 0; | offset = 0; | ||
| Line 150: | Line 150: | ||
if(count != str.count) return false; | if(count != str.count) return false; | ||
for(int i=0; i<count; i++){ | for(int i=0; i<count; i++){ | ||
if(value | if(value[offset+i] != str.value[str.offset + i]) return false; | ||
} | } | ||
return true; | return true; | ||
| Line 158: | Line 158: | ||
cout << "value : "; | cout << "value : "; | ||
for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
cout << value | cout << value[i]; | ||
cout << endl << "size : " << size << endl << "offset : " << offset << endl << "count : " << count << endl; | cout << endl << "size : " << size << endl << "offset : " << offset << endl << "count : " << count << endl; | ||
| Line 164: | Line 164: | ||
cout << "print : "; | cout << "print : "; | ||
for(int i=0; i < count; i++) | for(int i=0; i < count; i++) | ||
cout << value | cout << value[offset+i]; | ||
cout << endl << endl; | cout << endl << endl; | ||
} | } | ||
| Line 172: | Line 172: | ||
int indexOf(char c){ | int indexOf(char c){ | ||
for(int i=0; i < count; i++) | for(int i=0; i < count; i++) | ||
if(value | if(value[offset+i] == c) return i; | ||
return -1; | return -1; | ||
} | } | ||
| Line 178: | Line 178: | ||
char charAt(int index){ | char charAt(int index){ | ||
if(index > count || index < 0) return '\0'; | if(index > count || index < 0) return '\0'; | ||
else return value | else return value[offset+index]; | ||
} | } | ||
| Line 184: | Line 184: | ||
int index = -1; | int index = -1; | ||
for(int i=0; i<count; i++) | for(int i=0; i<count; i++) | ||
if(value | if(value[offset+i] == c) index = i; | ||
return index; | return index; | ||
} | } | ||
bool startsWith(const char* original){ | bool startsWith(const char* original){ | ||
for(int i=0; original | for(int i=0; original[i] != '\0'; i++){ | ||
if(i >= count) return false; | if(i >= count) return false; | ||
if(original | if(original[i] != value[offset+i]) return false; | ||
} | } | ||
return true; | return true; | ||
| Line 198: | Line 198: | ||
String concat(String &str){ | String concat(String &str){ | ||
int length = count + str.count; | int length = count + str.count; | ||
char *nvalue = new char | char *nvalue = new char[length+1]; | ||
for(int i=0; i<count; i++) nvalue | for(int i=0; i<count; i++) nvalue[i] = value[offset+i]; | ||
for(int i=0; i<str.count; i++) nvalue | for(int i=0; i<str.count; i++) nvalue[count+i] = str.value[str.offset+i]; | ||
nvalue | nvalue[length] = '\0'; | ||
return String(nvalue); | return String(nvalue); | ||
| Line 211: | Line 211: | ||
for(int i=firstIndex; i<count; i++){ | for(int i=firstIndex; i<count; i++){ | ||
if(value | if(value[i] != ' '){ | ||
firstIndex = i; | firstIndex = i; | ||
break; | break; | ||
| Line 217: | Line 217: | ||
} | } | ||
for(int i=lastIndex; i >= offset; i--){ | for(int i=lastIndex; i >= offset; i--){ | ||
if(value | if(value[i] != ' '){ | ||
lastIndex = i+1; | lastIndex = i+1; | ||
break; | break; | ||
| Line 271: | Line 271: | ||
return 0; | return 0; | ||
} | } | ||
Latest revision as of 00:44, 27 March 2026
- 사용 언어 : C++
String str("abcdef"); - 완료 String str2("abcb"); String str3(" aB c "); String str4(str); - 완료
| 함수 | 예 | 완수 여부 |
| charAt | str.charAt(3) == 'd' | 완료 |
| compareTo | str2.compareTo("abcb") == 0 | |
| compareToIgnoreCase | str2.compareTo("AbCb") == 0 | |
| concat | str.concat(str) == "abcdefabcdef" | |
| contains | str.contains("bcd") == TRUE | |
| endsWith | str.endsWith("ef") == TRUE | |
| startsWith | str.startsWith("abc") == TRUE | 완료 |
| equals | str.equals(new String("abcdef")) == TRUE | 완료 |
| equalsIgnoreCase | str.equalsIgnoreCase(new String("ABcdEf")) == TRUE | |
| indexOf | str2.indexOf("b") == 1 | 완료 |
| isEmpty | str.isEmpty == FALSE | 완료 |
| lastIndexOf | str2.lastIndexOf("b") == 3 | 완료 |
| length | str.length() == 6 | 완료 |
| replace | str.replace("bc", "ad") == "aaddef" | |
| split | str.split("c") == {"ab", "def"} | |
| subString | str.subString(2, 4) == "cd" | 완료 |
| format | String::format("참석자 : %d명", 8) == "참석자 : 8명" | |
| trim | str3.trim() == "aB c" | 완료 |
| toLower | str3.toLower() == " ab c " | |
| toUpper | str.toUpper() == "ABCDEF" | |
| valueOf | String::valueOf(3) == "3" |
소스 코드 =
#include <iostream>
using namespace std;
class String{
private:
char* value;
int offset;
int count;
int size;
public:
String(){
value = NULL;
offset = 0;
count = 0;
size = 0;
}
String(const char* original){
// 길이 파악
size = 0;
for(;original[size] != '\0'; size++);
count = size;
// value에 데이터 복사
value = new char[size];
for(int i=0; i < size; i++)
value[i] = original[i];
offset = 0;
}
String(String& str, int o, int c){
value = str.value;
size = str.size;
offset = o>=size? 0: o;
count = c>size? size: c;
}
String subString(int off1, int off2){
if(off1 < 0 || off2 < 0 || off1 > off2) return String();
String str(*this, offset+off1, off2-off1);
return str;
}
int length() { return size; }
bool equals(String &str){
if(count != str.count) return false;
for(int i=0; i<count; i++){
if(value[offset+i] != str.value[str.offset + i]) return false;
}
return true;
}
void print(){
cout << "value : ";
for(int i=0; i<size; i++)
cout << value[i];
cout << endl << "size : " << size << endl << "offset : " << offset << endl << "count : " << count << endl;
cout << "print : ";
for(int i=0; i < count; i++)
cout << value[offset+i];
cout << endl << endl;
}
bool isEmpty(){ return (count == 0 || offset > size ); }
int indexOf(char c){
for(int i=0; i < count; i++)
if(value[offset+i] == c) return i;
return -1;
}
char charAt(int index){
if(index > count || index < 0) return '\0';
else return value[offset+index];
}
int lastIndexOf(char c){
int index = -1;
for(int i=0; i<count; i++)
if(value[offset+i] == c) index = i;
return index;
}
bool startsWith(const char* original){
for(int i=0; original[i] != '\0'; i++){
if(i >= count) return false;
if(original[i] != value[offset+i]) return false;
}
return true;
}
String concat(String &str){
int length = count + str.count;
char *nvalue = new char[length+1];
for(int i=0; i<count; i++) nvalue[i] = value[offset+i];
for(int i=0; i<str.count; i++) nvalue[count+i] = str.value[str.offset+i];
nvalue[length] = '\0';
return String(nvalue);
}
String trim(){
int firstIndex = offset;
int lastIndex = offset+count-1;
for(int i=firstIndex; i<count; i++){
if(value[i] != ' '){
firstIndex = i;
break;
}
}
for(int i=lastIndex; i >= offset; i--){
if(value[i] != ' '){
lastIndex = i+1;
break;
}
}
return String(*this, firstIndex, lastIndex-firstIndex);
}
};
int main(void){
String str1("abcdef");
str1.print();
String str2(str1, 3, 2);
str2.print();
String str3 = str1.subString(3, 5);
str3.print();
String str4("abcb");
str4.print();
String str5 = str1.concat(str1);
str5.print();
String str6(" ab e ");
str6.print();
str6 = str6.trim();
str6.print();
String str7("abcdefgh");
String str8("de");
//cout << str7.subString(2, 6).subString(1, 3).equals(str8) << endl;
cout << "str2 == str3? : " << str2.equals(str3) << endl;
cout << "str1 indexOf b : " << str1.indexOf('b') << endl;
cout << "str1 charAt 1 : " << str1.charAt(3) << endl;
cout << "str4 lastIndexOf b : " << str4.lastIndexOf('b') << endl;
cout << "str1 startsWith abc? : " << str1.startsWith("abc") << endl;
cout << "str4 startsWith abc? : " << str4.startsWith("abc") << endl;
cout << "str2 startsWith abc? : " << str2.startsWith("abc") << endl;
//printf("%d", '0');
return 0;
}