daily pastebin goal
0%
SHARE
TWEET

Untitled

a guest Mar 24th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Chapter 11, exercise 06: read text file, replace punctuation with
  2. // whitespace, replace "don't" with "do not", "can't" with "cannot" etc.
  3. // Leave hyphens within words intact
  4. // Convert all characters to lower case
  5.  
  6. #include "pch.h"
  7. #include <iostream>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <string>
  11. #include <vector>
  12. #include <exception>
  13. #include <algorithm>
  14.  
  15. std::vector<std::string> read_text_from_file(std::string& fn)
  16. {
  17.     std::string line;
  18.     std::vector<std::string> text;
  19.     std::ifstream ifs(fn.c_str());
  20.     if (!ifs) std::cerr<<"Cannot open the file: "<<fn;
  21.     //for (size_t i = 0; i < text.size(); i++)
  22.     while(std::getline(ifs,line))
  23.         text.push_back(line);
  24.     return text;
  25. }
  26.  
  27. void to_lower(std::string& s)
  28. {
  29.     //if (s.size() == 0) return;    // skipt empty line
  30.     for (size_t i = 0; i < s.size(); ++i)
  31.         s[i] = tolower(s[i]);
  32. }
  33.  
  34. // change string to lower case
  35. void to_lowercase(std::vector<std::string>& v)
  36. {
  37.     std::string s;
  38.     for (size_t i = 0; i < v.size(); i++)
  39.     {
  40.         std::istringstream is(v[i]);
  41.         if (is >> s)
  42.             to_lower(v[i]);
  43.     }
  44. }
  45.  
  46. // change string to lower case
  47. void to_lowercase(std::vector<std::string>& v)
  48. {
  49.     std::string s;
  50.     for (size_t i = 0; i < v.size(); i++)
  51.     {
  52.         std::istringstream is(v[i]);
  53.         if (is >> s)
  54.             to_lower(v[i]);
  55.     }
  56. }
  57.  
  58.  
  59. // remove punctuation except ' and -
  60. void remove_punct()
  61. {
  62.     ;
  63. }
  64.  
  65. // replace "don't" with "do not", "can't" with "cannot" etc.
  66. // removes extra spaces between words
  67. void expand_aux(std::string& s)
  68. {
  69.     ;
  70. }
  71.  
  72. int main()
  73. {
  74.     std::cout << "Enter the file name\n";
  75.     std::string file_name;
  76.     std::cin >> file_name;
  77.     std::vector<std::string> changed_text;
  78.     changed_text = read_text_from_file(file_name);
  79.     to_lowercase(changed_text);
  80.     for (size_t i = 0; i < changed_text.size(); i++)
  81.     {
  82.         std::cout << changed_text[i];
  83.     }
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top