Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. // ConsoleApplication1.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "iostream"
  6. #include "string"
  7.  
  8. std::string FindWord(char words[], int word_number)
  9. {
  10.     std::string word = "";
  11.     int word_begin = 0;
  12.     int space_count = 0;
  13.     for (int i = 0; i < 100; i++)
  14.     {
  15.         if (word_number == 1)
  16.             break;
  17.         if (words[i] == ' ')
  18.         {
  19.             space_count++;
  20.             if (space_count == word_number - 1)
  21.             {
  22.                 word_begin = i + 1;
  23.                 break;
  24.             }
  25.         }
  26.     }
  27.     while (words[word_begin] != ' ' && words[word_begin] != 0)
  28.     {
  29.         word += words[word_begin];
  30.         word_begin++;
  31.     }
  32.     return word;
  33. }
  34.  
  35. int main()
  36. {
  37.     char words[100];
  38.     std::cin.getline(words, 100);
  39.     std::cout << words << '\n';
  40.     int word_number;
  41.     int count = 0;
  42.     std::cin >> word_number;
  43.     for (char i : words)
  44.     {
  45.         if (i == ' ')
  46.             count++;
  47.     }
  48.     if (word_number >= 1 && word_number <= count + 1)
  49.     {
  50.         std::string word = FindWord(words, word_number);
  51.         std::cout << word;
  52.         std::cout << '\n';
  53.     }
  54.     else
  55.     {
  56.         std::cout << "Enter a valid number";
  57.     }
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement