forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |