fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int x;
  6. char s[1005];
  7.  
  8. cin >> x;
  9. cin.ignore();
  10. cin.getline(s, 1005);
  11.  
  12. int len = strlen(s);
  13. int wordCount = 0;
  14. char word[1005];
  15. int pos = 0;
  16.  
  17. for(int i = 0; i <= len; i++) {
  18. if(s[i] != ' ' && s[i] != '\0') {
  19. word[pos++] = s[i];
  20. } else {
  21. if(pos > 0) {
  22. word[pos] = '\0';
  23. wordCount++;
  24. if(wordCount == x) {
  25. cout << word;
  26. return 0;
  27. }
  28. pos = 0;
  29. }
  30. }
  31. }
  32.  
  33. if(pos > 0) {
  34. word[pos] = '\0';
  35. wordCount++;
  36. if(wordCount == x) {
  37. cout << word;
  38. return 0;
  39. }
  40. }
  41.  
  42. return 0;
  43. }
  44.  
  45.  
Success #stdin #stdout 0.01s 5288KB
stdin
4
codedream is the best
stdout
best