fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. std::string findSentence(
  6. unsigned const stringIndex,
  7. unsigned const sentenceIndex,
  8. std::vector<std::string> const& stringArray,
  9. char const deliminator = '\n')
  10. {
  11. auto result = std::string{ "" };
  12.  
  13. if(stringIndex < stringArray.size())
  14. {
  15. auto index = unsigned{ 0 };
  16. auto posStart = std::string::size_type{ 0 };
  17. auto posEnd = stringArray[stringIndex].find(deliminator);
  18.  
  19. while((posEnd != std::string::npos) && (index < sentenceIndex))
  20. {
  21. posStart = posEnd + 1;
  22. posEnd = stringArray[stringIndex].find(deliminator, posStart);
  23. index++;
  24. }
  25.  
  26. if(index == sentenceIndex)
  27. {
  28. result = stringArray[stringIndex].substr(posStart, (posEnd - posStart));
  29. }
  30. }
  31.  
  32. return result;
  33. }
  34.  
  35. int main(int argc, char** argv)
  36. {
  37. std::vector<std::string> myArray{
  38. "This is the first string.",
  39. "This is the second string.",
  40. "This is just an example.\n"
  41. "This is the 2nd sentence in the paragraph.\n"
  42. "This is the 3rd sentence in the paragraph.",
  43. "This is the fourth string."
  44. };
  45.  
  46. std::cout << "0 Sentence of 0 String: " << findSentence(0, 0, myArray) << std::endl;
  47. std::cout << "1 Sentence of 0 String: " << findSentence(0, 1, myArray) << std::endl;
  48. std::cout << "2 Sentence of 0 String: " << findSentence(0, 2, myArray) << std::endl;
  49. std::cout << "\n" << std::endl;
  50. std::cout << "0 Sentence of 1 String: " << findSentence(1, 0, myArray) << std::endl;
  51. std::cout << "1 Sentence of 1 String: " << findSentence(1, 1, myArray) << std::endl;
  52. std::cout << "2 Sentence of 1 String: " << findSentence(1, 2, myArray) << std::endl;
  53. std::cout << "\n" << std::endl;
  54. std::cout << "0 Sentence of 2 String: " << findSentence(2, 0, myArray) << std::endl;
  55. std::cout << "1 Sentence of 2 String: " << findSentence(2, 1, myArray) << std::endl;
  56. std::cout << "2 Sentence of 2 String: " << findSentence(2, 2, myArray) << std::endl;
  57. std::cout << "\n" << std::endl;
  58. std::cout << "0 Sentence of 3 String: " << findSentence(3, 0, myArray) << std::endl;
  59. std::cout << "1 Sentence of 3 String: " << findSentence(3, 1, myArray) << std::endl;
  60. std::cout << "2 Sentence of 3 String: " << findSentence(3, 2, myArray) << std::endl;
  61. std::cout << "\n" << std::endl;
  62. std::cout << "0 Sentence of 4 String: " << findSentence(4, 0, myArray) << std::endl;
  63. std::cout << "1 Sentence of 4 String: " << findSentence(4, 1, myArray) << std::endl;
  64. std::cout << "2 Sentence of 4 String: " << findSentence(4, 2, myArray) << std::endl;
  65.  
  66. return 0;
  67. }
Success #stdin #stdout 0s 16072KB
stdin
Standard input is empty
stdout
0 Sentence of 0 String: This is the first string.
1 Sentence of 0 String: 
2 Sentence of 0 String: 


0 Sentence of 1 String: This is the second string.
1 Sentence of 1 String: 
2 Sentence of 1 String: 


0 Sentence of 2 String: This is just an example.
1 Sentence of 2 String: This is the 2nd sentence in the paragraph.
2 Sentence of 2 String: This is the 3rd sentence in the paragraph.


0 Sentence of 3 String: This is the fourth string.
1 Sentence of 3 String: 
2 Sentence of 3 String: 


0 Sentence of 4 String: 
1 Sentence of 4 String: 
2 Sentence of 4 String: