Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <vector>
- using namespace std;
- int main()
- {
- SetConsoleOutputCP(1251);
- const int MAX = 100;
- int count_spaces{}, i{}, length{}, words_length{};
- string local_word{};
- char sentence[MAX]{};
- vector<string> words{};
- while (true) {
- count_spaces = 0;
- cout << "Введіть будь-яке речення з парною кількістю слів: ";
- cin.getline(sentence, MAX, '\n');
- for (i = 0; i < MAX; i++) {
- if (sentence[i] == ' ') {
- count_spaces++;
- }
- }
- if (count_spaces % 2 == 0) {
- cout << "Необхідно ввести речення з парною кількістю слів. Спробуйте ще раз!" << endl;
- }
- else {
- break;
- }
- }
- for (i = 0; i < MAX; i++) {
- if (sentence[i] == ' ' || i == MAX - 1) {
- words.push_back(local_word);
- local_word = "";
- }
- else {
- local_word += sentence[i];
- }
- }
- words_length = size(words);
- for (i = 0; i < words_length; i++) {
- if (i % 2 != 0) {
- swap(words[i], words[i - 1]);
- }
- }
- for (i = 0; i < words_length; i++) {
- cout << words[i] << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement