fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <cctype>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. struct Word
  10. {
  11. string english; // English sentence
  12. string piglatin; // Pig latin sentence
  13. };
  14.  
  15. // PT 1. Function prototype
  16. Word* splitSentence(const string words, int &size);
  17.  
  18. int main()
  19. {
  20. string userSentence;
  21. int size;
  22.  
  23. // Get the users sentence to convert to pig latin
  24. cout << "Please enter a string to convert to pig latin:\n";
  25. getline(cin, userSentence);
  26.  
  27. // Directs to Word * splitSentence function
  28. Word* tempptr = splitSentence(userSentence, size);
  29.  
  30. for(int i = 0; i < size; ++i) {
  31. cout << tempptr[i].english << endl;
  32. }
  33.  
  34. delete [] tempptr;
  35.  
  36.  
  37. return 0;
  38. }
  39.  
  40. //PT 1. Analyze the sentence
  41. Word* splitSentence(const string words, int &size)
  42. {
  43. istringstream iss(words);
  44. vector<string> vec;
  45. string s;
  46.  
  47. while (iss >> s)
  48. {
  49. remove_if(s.begin(), s.end(),
  50. [](unsigned char ch){ return !isalpha(ch); });
  51.  
  52. if (!s.empty())
  53. {
  54. transform(s.begin(), s.end(), s.begin(),
  55. [](unsigned char ch){ return tolower(ch); });
  56.  
  57. vec.push_back(s);
  58. }
  59. }
  60.  
  61. Word *sentence = new Word[vec.size()];
  62.  
  63. transform(vec.begin(), vec.end(), sentence,
  64. [](const string &s){
  65. Word w;
  66. w.english = s;
  67. return w;
  68. }
  69. );
  70.  
  71. size = vec.size();
  72. return sentence;
  73. }
Success #stdin #stdout 0s 4308KB
stdin
The Quick Brown Fox Jumps Over The Lazy Dog
stdout
Please enter a string to convert to pig latin:
the
quick
brown
fox
jumps
over
the
lazy
dog