diff --git a/ch09/ex9_43.cpp b/ch09/ex9_43.cpp index 44badf82..0b6a4a4e 100644 --- a/ch09/ex9_43.cpp +++ b/ch09/ex9_43.cpp @@ -1,75 +1,35 @@ -//! @Alan -//! -//! Exercise 9.43: -//! Write a function that takes three strings, s, oldVal, and newVal. -//! Using iterators, and the insert and erase functions replace all instances of -//! oldVal that appear in s by newVal. -//! Test your function by using it to replace common abbreviations, -//! such as “tho” by “though” and “thru” by “through”. -//! +// +// ex9_43.cpp +// Exercise 9.43 +// +// Created by pezy on 12/4/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// @Brief Write a function that takes three strings, s, oldVal, and newVal. +// Using iterators, and the insert and erase functions replace +// all instances of oldVal that appear in s by newVal. +// Test your function by using it to replace common abbreviations, +// such as “tho” by “though” and “thru” by “through”. #include #include -#include -#include -#include -#include -//! -//! \brief Exercise 9.43 -//! @note Using iterators, and the insert and erase functions -//! \return 0 : replace operation happenned ; -1 : otherwise. -//! -int abbrv_handler(std::string&, const std::string&, const std::string&); -int main() -{ - std::string s("thru _ thru "); - abbrv_handler(s,"thru","through"); - std::cout << s; +using std::cout; using std::endl; using std::string; - return 0; -} -int abbrv_handler(std::string &s, const std::string &oldVal, const std::string &newVal) +void func(string &s, const string &oldVal, const string &newVal) { - if(oldVal.size() > s.size()) return -1; - - auto it = s.begin(); - while(it != s.end()) - { - if (*it == *oldVal.begin()) - { - //! @note build sub_str from s to compare the oldVal laterly - auto tmp = s.substr(it - s.begin(), oldVal.size()); - - if (tmp.compare(oldVal) == 0) - { - //! @note Store the offset has been checked - int offSet = it - s.begin(); - - - //! @note Remove [first,last) and update itterator using the return. - it = s.erase(it, it + oldVal.size()); - - - //! @note No proper iterator returned by all overloaded insert memeber. - s.insert(it, newVal.begin(), newVal.end()); - + auto found = s.find(oldVal); + if (found == string::npos) return; + s.erase(found, oldVal.size()); + s.insert(found, newVal); +} - //! @note Manually, move the iterator to point to the last of the added element: - //! Assuming "through" is newly added: - //! ********through****** - // ^ - //! above is where it points to. - //! by the statement ++it, it will move to the next element as shown below: - //! ********through****** - // ^ - it = s.begin() + offSet + oldVal.size() - 1; - } - } - ++it; - } +int main() +{ + string str{"To drive straight thru is a foolish, tho courageous act."}; + func(str, "tho", "though"); + func(str, "thru", "through"); + cout << str << endl; return 0; } - - diff --git a/ch09/ex9_44.cpp b/ch09/ex9_44.cpp index 0370b5c4..c1748e58 100644 --- a/ch09/ex9_44.cpp +++ b/ch09/ex9_44.cpp @@ -1,64 +1,31 @@ -//! @Alan -//! -//! Exercise 9.43: -//! Write a function that takes three strings, s, oldVal, and newVal. -//! Using iterators, and the insert and erase functions replace all instances of -//! oldVal that appear in s by newVal. -//! Test your function by using it to replace common abbreviations, -//! such as “tho” by “though” and “thru” by “through”. -//! -//! Exercise 9.44: -//! Rewrite the previous function using an index and replace. -//! +// +// ex9_44.cpp +// Exercise 9.44 +// +// Created by pezy on 12/4/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// @Brief Rewrite the previous function using an index and replace. +// @See 9.43 #include #include -//! Exercise 9.44 -std::string& -ex944(std::string &s, const std::string &oldVal, const std::string &newVal); +using std::cout; using std::endl; using std::string; -int main() +void func(string &s, const string &oldVal, const string &newVal) { - std::string s("thru _ thru "); - ex944(s,"thru","through"); - std::cout << s; - - return 0; + auto found = s.find(oldVal); + if (found == string::npos) return; + s.replace(found, oldVal.size(), newVal); } -//! Exercise 9.44 -std::string & -ex944(std::string &s, const std::string &oldVal, const std::string &newVal) +int main() { - //! @note Return without process,if size too large. - if(oldVal.size() > s.size()) return s; + string str{"To drive straight thru is a foolish, tho courageous act."}; + func(str, "tho", "though"); + func(str, "thru", "through"); + cout << str << endl; - std::string::size_type i=0; - while(i != s.size()) - { - if(s[i] == oldVal[0]) - { - //! @note build sub_str from s to compare the oldVal laterly - auto tmp = s.substr(i, oldVal.size()); - if (tmp.compare(oldVal) == 0) - { - s.replace(i,oldVal.size(),newVal); - - //! @note Manually, move the index to denote the last of the added element: - //! Assuming "through" is newly added: - //! ********through****** - // ^ - //! above is where the index i denotes now . - //! by the statement ++i , it will denote the next element as shown below: - //! ********through****** - // ^ - i = i + newVal.size() - 1; - } - } - ++i; - } - - return s; + return 0; } - diff --git a/ch09/ex9_45.cpp b/ch09/ex9_45.cpp index 20fd24b3..da311c65 100644 --- a/ch09/ex9_45.cpp +++ b/ch09/ex9_45.cpp @@ -19,7 +19,7 @@ pre_suffix(const std::string &name, const std::string &pre, const std::string &s int main() { std::string name("alan"); - std::cout << pre_suffix(name, "Mr.", "Jr."); + std::cout << pre_suffix(name, "Mr.", ",Jr."); return 0; } @@ -28,10 +28,9 @@ int main() inline std::string pre_suffix(const std::string &name, const std::string &pre, const std::string &su) { - auto ret = name; - ret.insert(ret.begin(), pre.begin(), pre.end()); + std::string ret(name); + ret.insert(0, pre); ret.append(su); return ret; } - diff --git a/ch09/ex9_46.cpp b/ch09/ex9_46.cpp new file mode 100644 index 00000000..ae38a69e --- /dev/null +++ b/ch09/ex9_46.cpp @@ -0,0 +1,31 @@ +// +// ex9_46.cpp +// Exercise 9.46 +// +// Created by pezy on 12/5/14. +// Copyright (c) 2014 pezy. All rights reserved. +// +// @Brief Rewrite the previous exercise using a position and length to manage the strings. +// This time use only the insert function. +// @See 9.45 + +#include +#include + +std::string +pre_suffix(const std::string &name, const std::string &pre, const std::string &su) +{ + std::string ret(name); + ret.insert(0, pre); + ret.insert(ret.size(), su); + + return ret; +} + +int main() +{ + std::string name("alan"); + std::cout << pre_suffix(name, "Mr.", ",Jr."); + + return 0; +} \ No newline at end of file