Advertisement
Guest User

Chris help me im dying

a guest
Oct 18th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. /*
  2. *
  3. *
  4. *
  5. *
  6. */
  7.  
  8. #include <iostream>
  9. #include <fstream>
  10. #include <vector>
  11. #include <string>
  12. using namespace std;
  13.  
  14.  
  15. vector<string> read_in(vector<string> words);
  16. int total_characters(vector<string> words);
  17. string longest_word(vector<string> words);
  18. string shortest_word(vector<string> words);
  19. int times_used(string input_word, vector<string> words);
  20. string word_before(string input_word, vector<string> words);
  21. string word_after(string input_word, vector<string> words);
  22.  
  23. int main(){
  24. vector<string> words;
  25. string input_word = "";
  26. double char_total = 0;
  27. double average = 0;
  28. string longest = "";
  29. string shortest = "";
  30. int word_used = 0;
  31. string before = "";
  32. ifstream in_file;
  33. ofstream out_file;
  34.  
  35. words = read_in(words);
  36. if(words.size() == 0){
  37. cout << "ERROR: Please use a valid .txt file"<< endl;
  38. return 0;
  39. }
  40. cout << "Please give a word with exact captialization: ";
  41. cin >> input_word;
  42. char_total = total_characters(words);
  43. average = char_total/words.size();
  44. longest = longest_word(words);
  45. shortest = shortest_word(words);
  46. word_used = times_used(input_word, words);
  47. before = word_before(input_word, words);
  48. cout << char_total << " " << average <<" " << longest <<" " << shortest <<" "<< word_used <<endl;
  49. }
  50.  
  51.  
  52. /*The purpose of this function is to read in information from a file
  53. *
  54. *Parameters:
  55. * vector<string>
  56. *
  57. *Return value:
  58. * This will return a value depending on whether or not the txt file exists
  59. */
  60. vector<string> read_in(vector<string> words){
  61. ifstream in_file;
  62. string file_name ="";
  63. string temp = "";
  64. cout << "Please give a file name: ";
  65. cin >> file_name;
  66. in_file.open(file_name.c_str());
  67. for(int i = 0; in_file >>temp; i++){
  68. words.push_back(temp);
  69. }
  70. return words;
  71. }
  72.  
  73.  
  74. /*This function will find the total amount of characters
  75. *
  76. *Parameters:
  77. * vector<string>
  78. *
  79. *return value:
  80. * This will return the amount of characters in a file
  81. */
  82. int total_characters(vector<string> words){
  83. string temp = "";
  84. int total=0;
  85. for(int i = 0; i < words.size(); i++){
  86. temp = words[i];
  87. total += temp.length();
  88. }
  89. return total;
  90. }
  91.  
  92. /*The purpose of this function is to find the longest word
  93. *
  94. *Parameters:
  95. * vector<string>
  96. *return value:
  97. * This returns the word that is the longest in the form of a string
  98. */
  99. string longest_word(vector<string> words){
  100. string longest = "";
  101. for(int i = 0; i < words.size(); i++){
  102. if( i == 0){
  103. longest = words[i];
  104. }
  105. if(words[i].length() > longest.length()){
  106. longest = words[i];
  107. }
  108. }
  109. return longest;
  110. }
  111.  
  112. /*The purpose of this function is to find the shortest word
  113. *
  114. *Parameters:
  115. * vector<string>
  116. *return value:
  117. * This returns the word that is the shortest in the form of a string
  118. */
  119. string shortest_word(vector<string> words){
  120. string shortest = "";
  121. for(int i = 0; i < words.size(); i++){
  122. if( i == 0){
  123. shortest = words[i];
  124. }
  125. if(words[i].length() < shortest.length()){
  126. shortest = words[i];
  127. }
  128. }
  129. return shortest;
  130. }
  131.  
  132. /*This function determines how many times a word is used that is given by the user
  133. *
  134. *Parameters:
  135. * input_word: the word the user gives, vector<string> words: The entire file in the form of strings
  136. *
  137. *return value:
  138. * How many times a certain string is used with exact capitalization
  139. */
  140. int times_used(string input_word, vector<string> words){
  141. int counter = 0;
  142. for(int i = 0; i < words.size(); i++){
  143. if(words[i] == input_word){
  144. counter++;
  145. }
  146. }
  147. return counter;
  148. }
  149.  
  150.  
  151. string word_before(string input_word, vector<string> words){
  152.  
  153. }
  154.  
  155. string word_after(string input_word, vector<string> words){
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement