2.char turidagi A satrdagi har bir soʻzni teng yarmini B satrdagi har bir soʻzning yarmiga qoʻshib S tarni hosil qiluvchi dastur tuzing. Dastur kodi: #include #include #include #include std::string hosil_qiluvchi_dastur(const std::string& A, const std::string& B) {
std::istringstream streamA(A);
std::istringstream streamB(B);
std::ostringstream streamS;
std::vector A_sozlar;
std::vector B_sozlar;
// A satridagi so'zlar
std::string sozA;
while (streamA >> sozA) {
A_sozlar.push_back(sozA);
}
// B satridagi so'zlar
std::string sozB;
while (streamB >> sozB) {
B_sozlar.push_back(sozB);
}
// A va B satrlarining yarmlarini qo'shish
for (size_t i = 0; i < A_sozlar.size() && i < B_sozlar.size(); ++i) {
std::string yarmiA = A_sozlar[i].substr(0, A_sozlar[i].length() / 2);
std::string yarmiB = B_sozlar[i].substr(0, B_sozlar[i].length() / 2);
streamS << yarmiA + yarmiB << " ";
}
return streamS.str().substr(0, streamS.str().length() - 1);
}
int main() {
// Test uchun satrlar
std::string A = "salom dunyo";
std::string B = "xabarlar salom";
// Hosil qilingan S satrini chiqarish
std::string S = hosil_qiluvchi_dastur(A, B);
// Natijani chiqarish
std::cout << "S = " << S << std::endl;
return 0;
}
Dastur natijasi:
Satrli massivlar (String). 3.String turidagi A satrda nechta har xil soʻz borligini aniqlovchi dastur tuzing.
Dastur kodi: #include #include #include
int countUniqueWords(const std::string& inputString) {
std::istringstream iss(inputString);
std::unordered_set uniqueWords;
std::string word;
while (iss >> word) {
for (char& ch : word) {
ch = std::tolower(ch);
}
uniqueWords.insert(word);
}
return uniqueWords.size();
}
int main() {
std::string inputString;
std::cout << "Feruzbek so'zlarni kirit: ";
std::getline(std::cin, inputString);
int uniqueWordsCount = countUniqueWords(inputString);
std::cout << "Xar xil so'zlar soni: " << uniqueWordsCount << std::endl;
return 0;
}