last_wish
Junior Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору
Код: #include <iostream> class stroka { char str[1024]; public: stroka(char *s); stroka(const stroka &s); int findword(char *s); bool addword(char *s, int position); char *show(); }; stroka::stroka(char *s) { char *it = s, *it2 = str; do { *it2++ = *it++; } while(*it); *it2 = '\0'; } stroka::stroka(const stroka &s) { char *it = (char *)s.str, *it2 = (char *)str; do { *it2++ = *it++; } while(*it); *it2 = '\0'; } int stroka::findword(char *s) { char *it; int startpos = 0, pos; bool isEq; while ((startpos < 1024) && (*(str + startpos))) { pos = startpos; isEq = false; it = s; while ((*it != '\0') && (pos < 1024)) { isEq = true; if ((*it++) != str[pos++]) { isEq = false; break; } } if ((isEq) && (*it == '\0')) return startpos; ++startpos; } return -1; } // добавление - это перезапись? если да, то: bool stroka::addword(char *s, int position) { if ((position >= 0) && (position < 1023)) { char *it = &str[position]; char *it2 = s; do { *it++ = *it2++; } while ((*it2 != '\0') && (position < 1023)); *it = '\0'; return true; } return false; } char* stroka::show() { return str; } int main() { stroka str1("Privet, bober!"); std::cout << str1.show() << std::endl; std::cout << str1.findword("bob") << std::endl; str1.addword("zamechatelnii bober!", str1.findword("bober")); std::cout << str1.show() << std::endl; return 0; } |
| Всего записей: 109 | Зарегистр. 26-10-2006 | Отправлено: 00:00 29-09-2007 | Исправлено: last_wish, 00:01 29-09-2007 |
|