-
Notifications
You must be signed in to change notification settings - Fork 16
add string insert #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,7 @@ void stringPrint(TString s); | |
| void stringDebug(TString s); | ||
|
|
||
| void stringRemoveChar(TString *s, char c); | ||
| void stringInsert(TString *s, size_t pos, TString toInsert); | ||
| void stringSwap(TString *s1, TString *s2); | ||
| void stringPushBack(TString *s, char c); | ||
| void stringPushFront(TString *s, char c); | ||
|
|
@@ -664,6 +665,27 @@ void stringRemoveChar(TString *s, char c) { | |
| s->size = newSize; | ||
| } | ||
|
|
||
| void stringInsert(TString *s, size_t pos, TString toInsert) { | ||
| if (pos >= s->size) return; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В таком случае стоит поставить ошибку перед завершением. Используйте clearError и setError |
||
|
|
||
| size_t substringLength = toInsert.size; | ||
| size_t newSize = s->size + substringLength; | ||
|
|
||
| TString secondPartCopy = stringInit(s->size - pos); | ||
| secondPartCopy.size = s->size - pos; | ||
| for (size_t i = pos; i < s->size; ++i) { | ||
| secondPartCopy.data[i - pos] = s->data[i]; | ||
| } | ||
|
|
||
| for (size_t i = pos; i < pos + substringLength; ++i) { | ||
| s->data[i] = toInsert.data[i - pos]; | ||
| } | ||
| for (size_t i = pos + substringLength; i < newSize; ++i) { | ||
| s->data[i] = secondPartCopy.data[i - substringLength - pos]; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| s->size = newSize; | ||
| } | ||
|
|
||
| void stringSwap(TString *s1, TString *s2) { | ||
| if (s1 == NULL || s2 == NULL) { | ||
| setError(ERR_NULL_POINTER); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Добавьте тесты в файл по образцу, чтобы протестировать новую функцию