fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string userSent, word1 = "", word2 = "", word3 = "", word4 = "";
  10. string word5 = "", suff1 = "", suff2 = "", suff3 = "";
  11. string subject, object, adj, verb, question, sentence;
  12. int index=0, sentLength;
  13.  
  14.  
  15. //Request the Eng-- sentence from the user
  16. cout << "Enter an Eng-- sentence you would like to translate" << endl;
  17. getline(cin, userSent);
  18. sentLength = userSent.length();
  19.  
  20. //Word 1
  21. while(userSent[index] != ' ' && index <= sentLength)
  22. {
  23. if (index == sentLength)
  24. {
  25. cout << "This is not a proper Eng-- sentence" << endl;
  26. return 0;
  27. }
  28. else
  29. {
  30. word1 += userSent[index];
  31. index++;
  32. }
  33. }
  34.  
  35. //Cycle through the spaces between word 1 and word 2
  36. while(userSent[index] == ' ')
  37. {
  38. index++;
  39. }
  40.  
  41. if (index == sentLength)
  42. {
  43. cout << "Not a proper Eng-- sentence" << endl;
  44. return 0;
  45. }
  46.  
  47. //Word 2
  48. //set the prevIndex to the index of the first letter
  49. //and collect the index for the second word
  50. while(userSent[index] != ' ')
  51. {
  52. if (index == sentLength)
  53. {
  54. cout << "This is not a proper Eng-- sentence" << endl;
  55. return 0;
  56. }
  57. else
  58. {
  59. word2 += userSent[index];
  60. index++;
  61. }
  62. }
  63.  
  64. //Cycle through the spaces between word 2 and word 3
  65. while(userSent[index] == ' ')
  66. {
  67. index++;
  68. }
  69.  
  70. if (index == sentLength)
  71. {
  72. cout << "Not a proper Eng-- sentence" << endl;
  73. return 0;
  74. }
  75.  
  76. //Word 3
  77. while(userSent[index] != ' ' && index <= sentLength)
  78. {
  79. word3 += userSent[index];
  80. index++;
  81. }
  82.  
  83. //Words greater than 3
  84. if (index < sentLength)
  85. {
  86. while(userSent[index] == ' ')
  87. {
  88. index++;
  89. }
  90. //Assigning the 4th word to word4
  91. while(userSent[index] != ' ' && index <= sentLength)
  92. {
  93. word4 += userSent[index];
  94. index++;
  95. }
  96. //Checking if there is a 5th word and assigning it to word5
  97. if (index < sentLength)
  98. {
  99. while(userSent[index] == ' ')
  100. {
  101. index++;
  102. }
  103.  
  104. while(userSent[index] != ' ' && index <=sentLength)
  105. {
  106. word5 += userSent[index];
  107. index++;
  108. }
  109. }
  110. }
  111.  
  112. //If there are more than 5 words, not an eng-- sentence
  113. if(word5 != "" && index < sentLength)
  114. {
  115. cout << "This is not a proper Eng-- sentence." << endl;
  116. return 0;
  117. }
  118.  
  119. //======================================================================
  120. //======================================================================
  121. //======================================================================
  122. // TESTING WORD COLLECTION
  123. //======================================================================
  124. //======================================================================
  125. //======================================================================
  126. cout << endl << "These are the words collected" << endl;
  127. cout << "Spaces after the colons and period at end of word are added." << endl;
  128. if (word1 != "")
  129. {
  130. cout << "First word: " <<word1<<"."<< endl;
  131. }
  132. if (word2 != "")
  133. {
  134. cout << "Second word: " <<word2<<"."<< endl;
  135. }
  136. if (word3 != "")
  137. {
  138. cout << "Third word: " <<word3<<"."<< endl;
  139. }
  140. if (word4 != "")
  141. {
  142. cout << "Fourth word: " <<word4<<"."<< endl;
  143. }
  144. if (word5 != "")
  145. {
  146. cout << "Fifth word: " <<word5<<"."<< endl;
  147. }
  148. cout << endl;
  149.  
  150.  
  151. //======================================================================
  152. //======================================================================
  153. //======================================================================
  154. // Assigning the word#'s grammatically
  155. //======================================================================
  156. //======================================================================
  157. //======================================================================
  158.  
  159. //Questions can have the following forms
  160. // is woman red
  161. // is the man a fish
  162. // is the woman strong
  163.  
  164.  
  165.  
  166. if (word1 == "is")
  167. {
  168. question = "is-ka";
  169.  
  170. //If the second word is a subject, assign it and continue
  171. //assigning the 3rd or 4th word as the object and kick out
  172. //an error if not
  173. if (word2 == string("man") || word2 == string("woman") || word2 == string("fish"))
  174. {
  175. subject = word2 + "-ga";
  176.  
  177. if (word3 == "man" || word3 == "woman" || word3 == "fish")
  178. {
  179. object = word3 + "-o";
  180. sentence = subject + ' ' + object + ' ' + question;
  181. cout << sentence << endl;
  182. }
  183. if (word3 == "red" || word3 == "short" || word3 == "strong")
  184. {
  185. adj = word3;
  186. sentence = subject + ' ' + adj + ' ' + question;
  187. cout << sentence << endl;
  188. }
  189. else
  190. {
  191. cout << "This is not a proper Eng-- sentence." << endl;
  192. cout << "2 The sentence lacks a proper object." << endl;
  193. }
  194. }
  195. //If the second word is an article, shift to the third word and test
  196. //as above
  197. else if ((word2 == "a" || word2 == "the")&&(word3 == "man" || word3 == "woman" || word3 == "fish"))
  198. {
  199. subject = word3 + "-ga";
  200. if (word4 == "red" || word4 == "strong" || word4 == "short")
  201. {
  202. adj = word4;
  203. sentence = subject + ' ' + adj + ' ' + question;
  204. cout << sentence << endl;
  205. }
  206. //In case there is a 'is the man a fish' test!
  207. else if ((word4 == "a" || word4 == "the")&&(word5 == "man" || word5 == "woman" || word5 == "fish"))
  208. {
  209. adj = word5;
  210. sentence = subject + ' ' + object + ' ' + question;
  211. cout << sentence << endl;
  212. }
  213. else
  214. {
  215. cout << "This was not a proper Eng-- sentence." << endl;
  216. cout << "The sentence lacks a proper adjective" << endl;
  217. }
  218. }
  219. else
  220. {
  221. cout << "This was not a proper Eng-- sentence" << endl;
  222. cout << "The sentence lacks a proper subject" << endl;
  223. return 0;
  224. }
  225. }
  226. //IF the sentence does not begin with 'is', it must begin with subject
  227. //or article.
  228. else if (word1 == "woman" || word1 == "man" || word1 == "fish")
  229. {
  230. subject = word1 + "-ga";
  231.  
  232. if (word2 == "kissed" || word2 == "caught" || word2 == "is")
  233. {
  234. verb = word2;
  235. }
  236. else
  237. {
  238. cout << "This was not a proper eng-- sentence." << endl;
  239. cout << "The sentence lacks a proper verb." << endl;
  240. return 0;
  241. }
  242.  
  243. if (word3 == "man" || word3 == "woman" || word3 == "fish")
  244. {
  245. object = word3 + "-o";
  246. sentence = subject + ' ' + object + ' ' + verb;;
  247. cout << sentence << endl;
  248. }
  249. else if ((word3 == "a" || word3 == "the")&&(word4 == "man" || word4 == "woman" || word4 == "fish"))
  250. {
  251. object = word4 + "-o";
  252. sentence = subject + ' ' + object + ' ' + verb;
  253. cout << sentence << endl;
  254. }
  255. else
  256. {
  257. cout << "This was not a proper eng-- sentence." << endl;
  258. cout << "1 The sentence lacks a proper object." << endl;
  259. return 0;
  260. }
  261. }
  262. else if ((word1 == "a" || word1 == "the") && (word2 == "man" || word2 == "woman" || word2 == "fish"))
  263. {
  264. subject = word2 + "-ga";
  265.  
  266. if (word3 == "caught" || word3 == "kissed" || word3 == "is")
  267. {
  268. verb = word3;
  269.  
  270. }
  271. else
  272. {
  273. cout << "This was not a proper eng-- sentence." << endl;
  274. cout << "The sentence lacks a proper verb." << endl;
  275. return 0;
  276. }
  277.  
  278. if (word4 == "man" || word4 == "woman" || word4 == "fish")
  279. {
  280. object = word4 + "-o";
  281. sentence = subject + ' ' + object + ' ' + verb;
  282. cout << sentence << endl;
  283. }
  284. else if ((word4 == "a" || word4 == "the") && (word5 == "man" || word5 == "woman" || word5 == "fish"))
  285. {
  286. object = word5 + "-o";
  287. sentence = subject + ' ' + object + ' ' + verb;
  288. cout << sentence << endl;
  289. }
  290. else
  291. {
  292. cout << "This was not a proper eng-- sentence." << endl;
  293. cout << "The sentence lacks a proper object." << endl;
  294. return 0;
  295. }
  296. }
  297. else
  298. {
  299. cout << "This was not a proper eng-- sentence." << endl;
  300. cout << "The sentence lacks a proper subject." << endl;
  301. return 0;
  302. }
  303.  
  304. return 0;
  305. }
  306.  
  307.  
  308.  
  309.  
  310.  
Success #stdin #stdout 0s 3476KB
stdin
is man red
stdout
Enter an Eng-- sentence you would like to translate

These are the words collected
Spaces after the colons and period at end of word are added.
First word: is.
Second word: man.
Third word: red.

This is not a proper Eng-- sentence.
2 The sentence lacks a proper object.