Hello,
I am trying to replace one of the loops of each code by the appropriate calls to the std library generic function templates. In each case, a single C++ statement is all that's needed, with no if's or loops required. Now for sure on how to do the call or which loop I am looking for. Any help would be great.
Aquaman
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
/*** Special instructions for this assignment appear in comment lines
bracketed within /*** ... ***/
/***
The following function checks a character to see if it is an uppercase
alphabetic character. If so, it converts it to lowercase. Otherwise
it leaves it alone.
***/
char lowerCase (char c)
{
if ((c >= 'A') && (c <= 'Z'))
return c - 'A' + 'a';
else
return c;
}
/***
The next function applies that idea to every character in a string.
***/
string lower (string s)
{
for (int i = 0; i < s.length(); ++i)
s[i] = lowerCase(s[i]);
return s;
}
/***
Replace the loop in the function above by a call to a std generic
function.
***/
int main (int argc, char** argv)
{
if (argc != 2)
{
cerr << "Usage: " << argv[0] << " string" << cerr;
return -1;
}
string theString = argv[1];
cout << theString << " => " << lower(theString) << endl;
return 0;
}
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
/*** Special instructions for this assignment appear in comment lines
bracketed within /*** ... ***/
/***
In this file, you find my solution to the oneEditAway assignment,
together with the test driver from that assignment.
Your task is to replace the commented-out loop in oneEditAway by an
appropriate call to a generic std function, specifically replacing the
indicated portion in the line below the loop.
***/
bool oneEditAway (const std::string& word1,
const std::string& word2)
// returns true if word1 can be formed from word2 by a single character
// change, which could be any of the following:
// - by deleting a single character (from anywhere in word1),
// - adding a single character (anywhere in word1),
// - replacing any single existing character in word1 with a different
// character, or
// - by transposing (exchanging) any pair of adjacent characters in word1
{
// Find the first character
// position where the two words
// are different
int firstDifferencePos = 0;
/*** Replace this loop: ***/
while ((firstDifferencePos
< word1.length())
&& (firstDifferencePos
< word2.length())
&& (word1[firstDifferencePos] == word2[firstDifferencePos]))
++firstDifferencePos;
/*** by the following statement (remove the leading //) ***/
// firstDifferencePos = /*** insert your code here ***/.first - word1.begin();
if ((firstDifferencePos >= word1.length())
&& (firstDifferencePos >= word2.length()))
return false; // words are identical
// Tests for the 4 possible edits are all fairly simple.
// Copy one of the words into a temporary string. Make a single
// change (at the position where the two words differ). Check to
// see if that change produced the second word.
// Check: deleting from word1
if (firstDifferencePos < word1.length())
{
string temp = word1;
temp.erase(firstDifferencePos, 1);
if (temp == word2)
return true;
}
// Check: deleting from word2 (same as inserting into word1)
if (firstDifferencePos < word2.length())
{
string temp = word2;
temp.erase(firstDifferencePos, 1);
if (temp == word1)
return true;
}
// Check: replacing in word1
if ((firstDifferencePos < word1.length())
&& (firstDifferencePos < word2.length()))
{
string temp = word1;
temp[firstDifferencePos] = word2[firstDifferencePos];
if (temp == word2)
return true;
}
// Check: transposing in word1
if (firstDifferencePos + 1 < word1.length())
{
string temp = word1;
temp[firstDifferencePos] = word1[firstDifferencePos+1];
temp[firstDifferencePos+1] = word1[firstDifferencePos];
if (temp == word2)
return true;
}
return false;
}
// Test driver for edit1 function
// Invoke with two strings in the command line
int main (int argc, char** argv)
{
if (argc != 3)
{
cerr << "Usage: " << argv[0] << " string1 string2" << cerr;
return -1;
}
string word1 = argv[1];
string word2 = argv[2];
cout << "'" << word1 << "' and '" << word2 << "' are ";
if (oneEditAway(word1, word2))
cout << "1 edit apart." << endl;
else
cout << "not 1 edit apart." << endl;
return 0;
}
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
/*** Special instructions for this assignment appear in comment lines
bracketed within /*** ... ***/
/***
In this file, you find my solution to the oneEditAway assignment,
together with a main routine. The main program takes, from the command
line, a word and the name of a dictionary file (e.g., /usr/dict/words).
The program is supposed to list all words in the dictionary that are
oneEditAway from the given word.
Your task is to replace the commented-out loop in the main program by an
appropriate call to generic std functions.
***/
bool oneEditAway (const std::string& word1,
const std::string& word2)
// returns true if word1 can be formed from word2 by a single character
// change, which could be any of the following:
// - by deleting a single character (from anywhere in word1),
// - adding a single character (anywhere in word1),
// - replacing any single existing character in word1 with a different
// character, or
// - by transposing (exchanging) any pair of adjacent characters in word1
{
// Find the first character
// position where the two words
// are different
int firstDifferencePos = 0;
while ((firstDifferencePos
< word1.length())
&& (firstDifferencePos
< word2.length())
&& (word1[firstDifferencePos] == word2[firstDifferencePos]))
++firstDifferencePos;
if ((firstDifferencePos >= word1.length())
&& (firstDifferencePos >= word2.length()))
return false; // words are identical
// Tests for the 4 possible edits are all fairly simple.
// Copy one of the words into a temporary string. Make a single
// change (at the position where the two words differ). Check to
// see if that change produced the second word.
// Check: deleting from word1
if (firstDifferencePos < word1.length())
{
string temp = word1;
temp.erase(firstDifferencePos, 1);
if (temp == word2)
return true;
}
// Check: deleting from word2 (same as inserting into word1)
if (firstDifferencePos < word2.length())
{
string temp = word2;
temp.erase(firstDifferencePos, 1);
if (temp == word1)
return true;
}
// Check: replacing in word1
if ((firstDifferencePos < word1.length())
&& (firstDifferencePos < word2.length()))
{
string temp = word1;
temp[firstDifferencePos] = word2[firstDifferencePos];
if (temp == word2)
return true;
}
// Check: transposing in word1
if (firstDifferencePos + 1 < word1.length())
{
string temp = word1;
temp[firstDifferencePos] = word1[firstDifferencePos+1];
temp[firstDifferencePos+1] = word1[firstDifferencePos];
if (temp == word2)
return true;
}
return false;
}
string theWord;
bool isNotOneEditAway (string word)
{
return !oneEditAway(word, theWord);
}
int main (int argc, char** argv)
{
if (argc != 3)
{
cerr << "Usage: " << argv[0] << " word dictionaryFileName" << cerr;
return -1;
}
theWord = argv[1];
ifstream dictionary (argv[2]);
string similarWords[5000];
string word;
int numberOfWords = 0;
/*** replace the loop below... ***/
while (dictionary >> word) {
if (!isNotOneEditAway(word)) {
similarWords[numberOfWords] = word;
++numberOfWords;
}
}
/*** by a single C++ statement using std generic functions ***/
cout << "Similar words:\n ";
// Copy all words in the array, up to but not including the first
// empty string, to the standard output
copy (similarWords,
find(similarWords, similarWords+5000, string()),
ostream_iterator<string>(cout, "\n "));
cout << flush;
return 0;
}