Сатрлар ва кенгайтирилган белгилар C++ tili satrlarni yaratish va ularni boshqarish uchun standart satr klassini ishlatadi
Download 471.33 Kb.
|
N10. Сатрлар ва кенгайтирилган белгилар
Сатрлар ва кенгайтирилган белгиларC++ tili satrlarni yaratish va ularni boshqarish uchun standart satr klassini ishlatadi.
Қаторларни аниқлаш ва белгилашString classi obyektlari yakunlovchi simvolga ‘\0’ ega bo’lishlari shart emas #include #include #include using namespace std;int main(){string t;getline(cin, t);cout <return 0;}Initializing Strings
cout << message.length( ); // Output: 13Бирлашган қатор. CONCATENATING STRINGS// string2.cpp: Reads several lines of text and outputs in reverse order.#include #include using namespace std;int main( ){cout <<"Terminate the input with an empty line.\n" ;string text, satr; // Empty stringswhile(true){getline(cin, satr); // Reads a line of textif( satr.length() == 0) // Empty line?break; // Yes ->end of the looptext = satr + '\n' + text; // Inserts a new line at the beginning.}// Output:cout << satr << '\n'<< "Your lines of text in reverse order:"<<'\n'<cout << text ;return 0; }Yuqoridagi dasturda string klassida + va + = operatorlari (конкатенация) birlashtirish vazifasida kelmoqda, ==, ! =, <, <=,> va > = operatorlari satrlarni solishtirish uchun qo’llanilmoqda.
Using + to Concatenate Strings
string sum, s1(“Tosh"), s2(“baqa");sum = s1 + s2;
string s("Good morning ");cout << s + "mister X" + '!';Using += to Concatenate Strings
string s1("Good "), s2("luck!");s1 += s2; // To concatenate s2 and s1.s1 += "luck!"; // Also possiblecout << s1;Қаторларни солиштириш. COMPARING STRINGS#include #include using namespace std;int main(){string s1, s2;getline( cin, s1); // Read the firstgetline( cin, s2); // and second line.if( line1 == line2)cout << " Satrlar bir xil" << endl;else cout<<"Ular br xil emas";return 0; }#include #include #include using namespace std;int main(){string s1, s2;getline( cin, s1); // Read the firstgetline( cin, s2); // and second line.s1>s2?cout<return 0; }Solishtirish natijalari
Қатор қўшиш ва олиб ташлаш INSERTING AND ERASING IN STRINGSInserting a stringstring s1("Miss Summer");s1.insert(5, "Ashley "); // Insert at position: 5cout<Erasing a substringstring s("The summer-time");s.erase(4,7); // Start position: 4, Quantity: 7cout<string s("winter-story");string s("winter-story");s.erase(6); // s now contains "winter"cout<erase( ); argumentsizberilsa satr to’liqo’chiriladiҚатор ўрнини эгаллаш ва қатор қидируви. SEARCHING AND REPLACING IN STRINGSstring s1("There they go again!"),s2("Bob and Bill");s1.replace(6, 4, s2);cout<s1.replace(6, 4, s2); string s1("Here comes Mike!"),string s1("Here comes Mike!"),s2("my love?");s1.replace(11, 4, s2, 0, 7);cout<s1.replace(11, 4, s2, 0, 7); Searchingstring youth("Bill is so young, so young");int first = youth.find("young");cout<E’tiboringizuchunrahmat |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling