Advertisement
Guest User

MP6

a guest
Apr 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream> //allows access to the stream function
  4.  
  5. using namespace std;
  6.  
  7. //Part 1
  8. int countVowels(string input) // counts # of vowels in string input
  9. {
  10. int vowel = 0;
  11. for (int i = 0; i < input.size(); i++)
  12. {
  13. if (input[i] == 'a' || input[i] == 'A')
  14. {
  15. vowel++;
  16. }
  17. else if (input[i] == 'e' || input[i] == 'E')
  18. {
  19. vowel++;
  20. }
  21. else if (input[i] == 'i' || input[i] == 'I')
  22. {
  23. vowel++;
  24. }
  25. else if (input[i] == 'o' || input[i] == 'O')
  26. {
  27. vowel++;
  28. }
  29. else if (input[i] == 'u' || input[i] == 'U')
  30. {
  31. vowel++;
  32. }
  33. else if(input[i] == ' ')
  34. {
  35.  
  36. }
  37. else
  38. {
  39.  
  40. }
  41. }
  42.  
  43. return vowel;
  44. }
  45.  
  46. int countNonSpace(string input) //counts # of characters that aren't spaces
  47. {
  48. int nonSpace = 0;
  49. for (int i = 0; i < input.size(); i++)
  50. {
  51. if (input[i] == ' ')
  52. {
  53.  
  54. }
  55. else
  56. {
  57. nonSpace++;
  58. }
  59. }
  60.  
  61. return nonSpace;
  62. }
  63.  
  64. int countWords(string input) //counts # of words
  65. {
  66.  
  67. int wordCount = 1;
  68. for (int i = 0; i < input.size(); i++)
  69. {
  70. if (input[i] == ' ')
  71. {
  72. wordCount++;
  73. }
  74. else
  75. {
  76.  
  77. }
  78. }
  79. return wordCount;
  80. }
  81.  
  82. //Part 2
  83. void findWords(string input) //converts standard English into Pig Latin
  84. {
  85.  
  86. istringstream ss(input);
  87. string word;
  88.  
  89. while (ss >> word)
  90. {
  91. //begins with vowels
  92. if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u' || word[0] == 'A' || word[0] == 'E' || word[0] == 'I' || word[0] == 'O' || word[0] == 'U')
  93. {
  94. cout << word + "lay" << ' ';
  95. }
  96. else //begins with consonants
  97. {
  98. for (int i = 0; i < word.size(); i++)
  99. {
  100. //until word hits a vowel
  101. if(word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u' || word[i] == 'A' || word[i] == 'E' || word[i] == 'I' || word[i]== 'O' || word[i] == 'U' || word[i] == 'y' || word[i] == 'Y')
  102. {
  103. string part = word.substr(0, i);
  104. string phrase = word.erase(0, i);
  105. string newPhrase = phrase + part + "ay";
  106. cout << newPhrase << ' ';
  107. break;
  108.  
  109. }
  110. else
  111. {
  112.  
  113. }
  114.  
  115.  
  116. }
  117.  
  118. }
  119.  
  120. }
  121.  
  122. system("pause");
  123.  
  124.  
  125. }
  126.  
  127.  
  128.  
  129. int main()
  130. {
  131. string input;
  132. getline(cin, input); //gets the entire line of input
  133.  
  134. cout << "There are " << countVowels(input) << " vowels." << endl;
  135. cout << "There are " << countNonSpace(input) << " characters that are not spaces. " << endl;
  136. cout << "There are " << countWords(input) << " words. " << endl;
  137.  
  138. cout << "**********************************************************************" << endl;
  139.  
  140. findWords(input);
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement