praktikum_cpp1

Основы С++ / Тема 2 / Урок 6. Цикл for и выход из цикла / Задача 3

Sep 10th, 2020
6,765
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. string query;
  8. getline(cin, query);
  9.  
  10. string word;
  11. for (int i = 0; i < query.size(); ++i) {
  12. if (query[i] == ' ') {
  13. cout << '[' << word << ']' << endl;
  14. word = ""s;
  15. } else {
  16. word += query[i];
  17. }
  18. }
  19. cout << '[' << word << ']' << endl;
  20. }
Comments
  • lizalaver
    2 years
    # C++ 0.42 KB | 0 0
    1. #include <iostream>
    2. #include <string>
    3.  
    4. using namespace std;
    5.  
    6. int main() {
    7.     string query;
    8.     getline(cin, query);
    9.     int i;
    10.     string word;
    11.     for(i = 0; i < query.size(); i++){
    12.         if(query[i] != ' '){
    13.         word += query[i];
    14.         }
    15.            else{
    16.             cout << "["s << word << "]"s << endl;
    17.             word = ""s;
    18.            }
    19.     }
    20.     cout <<  "["s << word << "]"s << endl;
    21. }
  • delix777
    2 years
    # C++ 0.40 KB | 0 0
    1. #include <iostream>
    2. #include <string>
    3.  
    4. using namespace std;
    5.  
    6. int main() {
    7.     string query;
    8.     getline(cin, query);
    9.     string word{};
    10.  
    11.     for (int i = 0; i < query.size(); ++i) {
    12.         if (query[i] == ' ') {
    13.             cout << "["s << word << "]"s << endl;
    14.             word = ""s;
    15.         }else{
    16.             word += query[i];
    17.         }
    18.     }
    19.     cout << "["s << word << "]"s << endl;
    20. }
  • wacko_dead
    2 years
    # text 0.42 KB | 0 0
    1. #include <iostream>
    2. #include <string>
    3.  
    4. using namespace std;
    5.  
    6. int main() {
    7. string query;
    8. getline(cin, query);
    9. query += " "s;
    10. string output_data = ""s;
    11. for (int i = 0; i <= query.size(); ++i) {
    12. if (query[i] != ' ') {
    13. output_data += query[i];
    14. }
    15. else{
    16. cout<< "["s + output_data + "]"s <<endl;
    17. output_data = ""s;
    18. }
    19. }
    20. }
  • MJPlaysHD
    2 years (edited)
    # text 0.33 KB | 0 0
    1. #include <iostream>
    2. #include <string>
    3.  
    4. using namespace std;
    5.  
    6. int main() {
    7. string query , str ="["s;
    8. getline(cin, query);
    9. for(int i = 0 ; i <= query.size()-1; i++){
    10. if (query[i] == ' '){
    11. str+= "]\n["s;
    12. }else{
    13. str+= query[i];
    14. }
    15. }
    16. cout <<str +"]"<<endl;
    17. }
  • Im5G
    1 year
    # text 0.45 KB | 0 0
    1. // с использованием цикла while
    2. #include <iostream>
    3. #include <string>
    4.  
    5. using namespace std;
    6.  
    7. int main() {
    8. string query, word;
    9. getline(cin, query);
    10.  
    11. int i = 0;
    12. int s = query.size();
    13.  
    14. while (i <= s){
    15. if ((query[i] != ' ') && (i != s)){
    16. word += query[i];
    17. } else {
    18. cout << "["s << word << "]"s << endl;
    19. word = ""s;
    20. }
    21.  
    22. ++i;
    23. }
    24. }
  • nickzheleznov
    1 year
    # C++ 0.37 KB | 0 0
    1. #include <iostream>
    2. #include <string>
    3.  
    4. using namespace std;
    5.  
    6. int main() {
    7.     string query;
    8.     getline(cin, query);
    9.     string word;
    10.     for (int i = 0; i <= query.size(); ++i) {
    11.         if (query[i] != ' ' && i < query.size()) {
    12.             word += query[i];
    13.         } else {
    14.             cout << "[" << word << "]" << endl;
    15.             word = "";
    16.         }
    17.     }
    18. }
  • just-killy
    1 year
    # C++ 0.44 KB | 0 0
    1. #include <iostream>
    2. #include <string>
    3.  
    4. using namespace std;
    5.  
    6. int main() {
    7.     string query;
    8.     getline(cin, query);
    9.    
    10.     cout << "[";
    11.     string word;
    12.     for (int i = 0; i < query.size(); ++i)
    13.     {
    14.         word = query[i];
    15.        
    16.         if (word != " ")
    17.         {
    18.             cout << word;
    19.         } else
    20.         {
    21.             cout << "]" << endl << "[";
    22.         }
    23.        
    24.     }
    25.     cout << "]";
    26.     return 0;
    27. }
Add Comment
Please, Sign In to add comment