fork download
  1. #include <iostream>
  2. #include <cctype>
  3. #include <string>
  4. using namespace std;
  5.  
  6. struct Word
  7. {
  8. string english; // English sentence
  9. string piglatin; // Pig latin sentence
  10. };
  11.  
  12. // PT 1. Function prototype
  13. Word* splitSentence(const string words, int &size);
  14.  
  15. int main()
  16. {
  17. string userSentence;
  18. int size;
  19.  
  20. // Get the users sentence to convert to pig latin
  21. cout << "Please enter a string to convert to pig latin:\n";
  22. getline(cin, userSentence);
  23.  
  24. // Directs to Word * splitSentence function
  25. Word* tempptr = splitSentence(userSentence, size);
  26.  
  27. for(int i = 0; i < size; ++i) {
  28. cout << tempptr[i].english << endl;
  29. }
  30.  
  31. delete [] tempptr;
  32.  
  33. return 0;
  34. }
  35.  
  36. //PT 1. Analyze the sentence
  37. Word* splitSentence(const string words, int &size)
  38. {
  39. bool flag = true;
  40. int num = 0;
  41. char ch;
  42.  
  43. for (int i = 0; i < words.length(); ++i)
  44. {
  45. ch = words[i];
  46.  
  47. if (isalpha(ch))
  48. {
  49. if (flag)
  50. {
  51. flag = false;
  52. ++num;
  53. }
  54. }
  55. else if (isspace(ch))
  56. {
  57. flag = true;
  58. }
  59. }
  60.  
  61. Word *sentence = new Word[num];
  62. int index = -1;
  63.  
  64. flag = true;
  65. num = 0;
  66.  
  67. for (int i = 0; i < words.length(); ++i)
  68. {
  69. ch = words[i];
  70.  
  71. if (isalpha(ch))
  72. {
  73. if (flag)
  74. {
  75. flag = false;
  76. ++num;
  77. ++index;
  78. }
  79.  
  80. if (isupper(ch))
  81. {
  82. ch = tolower(ch);
  83. }
  84.  
  85. sentence[index].english += ch;
  86. }
  87. else if (isspace(ch))
  88. {
  89. flag = true;
  90. }
  91. }
  92.  
  93. size = num;
  94. return sentence;
  95. }
Success #stdin #stdout 0s 4412KB
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