Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Chapter 11, exercise 06: read text file, replace punctuation with
- // whitespace, replace "don't" with "do not", "can't" with "cannot" etc.
- // Leave hyphens within words intact
- // Convert all characters to lower case
- #include "pch.h"
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <vector>
- #include <exception>
- #include <algorithm>
- std::vector<std::string> read_text_from_file(std::string& fn)
- {
- std::string line;
- std::vector<std::string> text;
- std::ifstream ifs(fn.c_str());
- if (!ifs) std::cerr<<"Cannot open the file: "<<fn;
- //for (size_t i = 0; i < text.size(); i++)
- while(std::getline(ifs,line))
- text.push_back(line);
- return text;
- }
- void to_lower(std::string& s)
- {
- //if (s.size() == 0) return; // skipt empty line
- for (size_t i = 0; i < s.size(); ++i)
- s[i] = tolower(s[i]);
- }
- // change string to lower case
- void to_lowercase(std::vector<std::string>& v)
- {
- std::string s;
- for (size_t i = 0; i < v.size(); i++)
- {
- std::istringstream is(v[i]);
- if (is >> s)
- to_lower(v[i]);
- }
- }
- // change string to lower case
- void to_lowercase(std::vector<std::string>& v)
- {
- std::string s;
- for (size_t i = 0; i < v.size(); i++)
- {
- std::istringstream is(v[i]);
- if (is >> s)
- to_lower(v[i]);
- }
- }
- // remove punctuation except ' and -
- void remove_punct()
- {
- ;
- }
- // replace "don't" with "do not", "can't" with "cannot" etc.
- // removes extra spaces between words
- void expand_aux(std::string& s)
- {
- ;
- }
- int main()
- {
- std::cout << "Enter the file name\n";
- std::string file_name;
- std::cin >> file_name;
- std::vector<std::string> changed_text;
- changed_text = read_text_from_file(file_name);
- to_lowercase(changed_text);
- for (size_t i = 0; i < changed_text.size(); i++)
- {
- std::cout << changed_text[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement