Skip to content

Commit

Permalink
added 9.47 and refuctor 9.49
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 4, 2014
1 parent c3aef9b commit f35ccca
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 44 deletions.
8 changes: 8 additions & 0 deletions ch09/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,11 @@ Use member `reserve(120)` to allocate enough space for this string. (@Mooophy)
## [Exercise 9.44](ex9_44.cpp)
## [Exercise 9.45](ex9_45.cpp)
## [Exercise 9.46](ex9_46.cpp)
## Exercise 9.47 [find_first_of](ex9_47_1.cpp) | [find_first_not_of](ex9_47_2.cpp)
## Exercise 9.48:
>Given the definitions of name and numbers on page 365,
what does numbers.find(name) return?

string::npos

## [Exercise 9.49](ex9_49.cpp)
34 changes: 34 additions & 0 deletions ch09/ex9_47_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// ex9_47_1.cpp
// Exercise 9.47
//
// Created by pezy on 12/5/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief Write a program that finds each numeric character
// and then each alphabetic character in the string "ab2c3d7R4E6".
// Write two versions of the program. The first should use find_first_of,
// and the second find_first_not_of.
// @Version find_first_of

#include <string>
#include <iostream>

using std::string; using std::cout; using std::endl;

int main()
{
string numbers{"123456789"};
string alphabet{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string str{"ab2c3d7R4E6"};

cout << "numeric characters: ";
for (string::size_type pos = 0; (pos = str.find_first_of(numbers, pos)) != string::npos; ++pos)
cout << str[pos] << " ";
cout << "\nalphabetic characters: ";
for (string::size_type pos = 0; (pos = str.find_first_of(alphabet, pos)) != string::npos; ++pos)
cout << str[pos] << " ";
cout << endl;

return 0;
}
34 changes: 34 additions & 0 deletions ch09/ex9_47_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// ex9_47_2.cpp
// Exercise 9.47
//
// Created by pezy on 12/5/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief Write a program that finds each numeric character
// and then each alphabetic character in the string "ab2c3d7R4E6".
// Write two versions of the program. The first should use find_first_of,
// and the second find_first_not_of.
// @Version find_first_not_of

#include <string>
#include <iostream>

using std::string; using std::cout; using std::endl;

int main()
{
string numbers{"123456789"};
string alphabet{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string str{"ab2c3d7R4E6"};

cout << "numeric characters: ";
for (string::size_type pos = 0; (pos = str.find_first_not_of(alphabet, pos)) != string::npos; ++pos)
cout << str[pos] << " ";
cout << "\nalphabetic characters: ";
for (string::size_type pos = 0; (pos = str.find_first_not_of(numbers, pos)) != string::npos; ++pos)
cout << str[pos] << " ";
cout << endl;

return 0;
}
69 changes: 25 additions & 44 deletions ch09/ex9_49.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,36 @@
//! @Alan
//!
//! Exercise 9.49:
//! A letter has an ascender if, as with d or f, part of the
//! letter extends above the middle of the line. A letter has
//! a descender if, as with p or g, part of the letter extends
//! below the line.
//! Write a program that reads a file containing words and reports
//! the longest word that contains neither ascenders nor descenders.
//!
//
// ex9_49.cpp
// Exercise 9.49
//
// Created by pezy on 12/5/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief A letter has an ascender if, as with d or f, part of the letter extends
// above the middle of the line.
// A letter has a descender if, as with p or g, part of the letter extends below the line.
// Write a program that reads a file containing words and reports the longest word
// that contains neither ascenders nor descenders.

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <iostream>

//! Exercise 9.47
std::string
ex949(const std::string &fileName);
using std::string; using std::cout; using std::endl; using std::ifstream;

int main()
{
std::cout << ex949("test.txt");
return 0;
}


std::string
ex949(const std::string &fileName)
{
std::string word;
std::ifstream fin(fileName);
ifstream ifs("../data/letter.txt");
if (!ifs) return -1;
string longest_word;
for (string word; ifs >> word; )
if (word.find_first_not_of("aceimnorsuvwxz") == string::npos &&
word.size() > longest_word.size())
longest_word = word;

cout << longest_word << endl;

//! store into a vector
std::vector<std::string> v;
while (fin >> word)
{
if(word.find_first_not_of("aceimnorsuvwxz") == std::string::npos)
{
v.push_back(word);
}
}
ifs.close();

//! move the longest into the string result
std::string result;
if(v.size() != 0)
{
for(auto it = v.begin(); it != v.end(); ++it)
{
if((*it).size() > result.size()) result = *it;
}
}

return result;
return 0;
}
5 changes: 5 additions & 0 deletions data/letter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
In typography, an ascender is the portion of a minuscule letter in a Latin-derived alphabet that extends above the mean line of a font. That is, the part of a lower-case letter that is taller than the font's x-height.

Ascenders, together with descenders, increase the recognizability of words. For this reason, British road signs no longer use all capital letters.[1]

Studies made at the start of the construction of the British motorway network concluded that words with mixed-case letters were much easier to read than "all-caps" and a special font was designed for motorway signs. These then became universal across the U.K. See Road signs in the United Kingdom.

0 comments on commit f35ccca

Please sign in to comment.