Skip to content

Commit

Permalink
refuctor 9.43 to 9.45, added 9.46
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 4, 2014
1 parent 9b355f0 commit abf5ab9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 122 deletions.
90 changes: 25 additions & 65 deletions ch09/ex9_43.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>

//!
//! \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;
}


73 changes: 20 additions & 53 deletions ch09/ex9_44.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <string>

//! 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;
}

7 changes: 3 additions & 4 deletions ch09/ex9_45.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}

31 changes: 31 additions & 0 deletions ch09/ex9_46.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <string>

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;
}

0 comments on commit abf5ab9

Please sign in to comment.