#include #include using namespace std; int main() { string s; cout << "Satr kiriting" << endl; getline(cin, s); cout << "Siz kiritgan satr " << s.length() << " ta belgidan iborat"; cout << "Siz kiritgan satr " << s.size() << " ta belgidan iborat"; return 0; } Satr qismini almashtirish Satrning biror qismini almashtirish kerak bo'lsa, replace funksiyasidan foydalanish mumkin.
replace (unsigned int pos1, unsigned int n1, const string &str);
replace (unsigned int pos1, unsigned int n1, const string & str, unsigned int pos2, unsigned
int n2);
replace (unsigned int pos1, unsigned int n1, const char *str, int n);
replace funksiyasi insert kabi ishlaydi, faqat qo'shilishi kerak bo'lan satrni pos1 - o'rindan
boshlab n1 ta belgi o'rniga qo'shadi.
2 ta satrni to'la almashtirish uchun swap funksiyasi ishlatiladi.
#include #include using namespace std; int main() { string s = "Assalomu alaykum do'stlar"; string c = "Merhibon va muhtarama ayol"; cout << s << endl; // 17 - belgidan boshlab 5 ta belgi o'rniga c satrni qo'shish s.replace(17, 5, c); cout << s << endl; s.swap(c); // 2 ta satrni to'la almashtirish cout << s << endl; s.replace(0, 0, c, 0, 17); s.erase(25); cout << s << endl; return 0; }