A
arnuld
I am able to create the 90% of this program and it runs fine. In its
present implementation, it reads from standard input. I am not able to
complete this program as last part requires to read from a file. All I
know about file-streams is that I need to use:
<int main(int argc, char**argv)>
and nothing more than that. I will appreciate if someone can help me:
/* C++ Primer - 4/e
*
* chapter 11, exercise 11.9
* STATEMENT
* Write a program to count word size of greater than or equal to 4
including printing the list of unique words in the input. Test your
program by running it on program's source file.
*
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
/* this functions appends the 3rd argument to its 2nd argument if 1st
argument is true */
std::string make_plural( size_t ctr,
const std::string &word,
const std::string & ending )
{
return (ctr == 1) ? word : word + ending;
}
bool isShorter( const std::string &s1, const std::string &s2 ) {
return s1.size() < s2.size();
}
bool GT4( const std::string &s )
{
return s.size() >= 4;
}
int main( )
{
std::vector<std::string> svec;
/* input some words */
std::copy( std::istream_iterator<std::string>( std::cin ),
std::istream_iterator<std::string>(), std::back_inserter( svec ) );
/* copy the vector, to be used later for printing */
std::vector<std::string> svec_old( svec );
std::sort( svec.begin(), svec.end() );
/* to eliminate th dupilcate words we 1st, rearrange the words by
putting all the duplicate words in the end of vector and then we will
use vector operation ERASE to remove them */
std::vector<std::string>::iterator begin_duplicates =
std::unique( svec.begin(), svec.end() );
svec.erase( begin_duplicates, svec.end() );
/* sort the words by size while maintaining the alphabetical order */
std::stable_sort( svec.begin(), svec.end(), isShorter );
std::vector<std::string>::size_type unique_count =
std::count_if( svec.begin(), svec.end(), GT4 );
std::cout << unique_count << " "
<< make_plural( unique_count, "word", "s" )
<< " 4 characters or longer"
<< std::endl;
for( std::vector<std::string>::const_iterator iter = svec_old.begin();
iter != svec_old.end();
++iter )
{
if( GT4( *iter ))
{
std::cout << *iter << std::endl;
}
}
return 0;
}
present implementation, it reads from standard input. I am not able to
complete this program as last part requires to read from a file. All I
know about file-streams is that I need to use:
<int main(int argc, char**argv)>
and nothing more than that. I will appreciate if someone can help me:
/* C++ Primer - 4/e
*
* chapter 11, exercise 11.9
* STATEMENT
* Write a program to count word size of greater than or equal to 4
including printing the list of unique words in the input. Test your
program by running it on program's source file.
*
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
/* this functions appends the 3rd argument to its 2nd argument if 1st
argument is true */
std::string make_plural( size_t ctr,
const std::string &word,
const std::string & ending )
{
return (ctr == 1) ? word : word + ending;
}
bool isShorter( const std::string &s1, const std::string &s2 ) {
return s1.size() < s2.size();
}
bool GT4( const std::string &s )
{
return s.size() >= 4;
}
int main( )
{
std::vector<std::string> svec;
/* input some words */
std::copy( std::istream_iterator<std::string>( std::cin ),
std::istream_iterator<std::string>(), std::back_inserter( svec ) );
/* copy the vector, to be used later for printing */
std::vector<std::string> svec_old( svec );
std::sort( svec.begin(), svec.end() );
/* to eliminate th dupilcate words we 1st, rearrange the words by
putting all the duplicate words in the end of vector and then we will
use vector operation ERASE to remove them */
std::vector<std::string>::iterator begin_duplicates =
std::unique( svec.begin(), svec.end() );
svec.erase( begin_duplicates, svec.end() );
/* sort the words by size while maintaining the alphabetical order */
std::stable_sort( svec.begin(), svec.end(), isShorter );
std::vector<std::string>::size_type unique_count =
std::count_if( svec.begin(), svec.end(), GT4 );
std::cout << unique_count << " "
<< make_plural( unique_count, "word", "s" )
<< " 4 characters or longer"
<< std::endl;
for( std::vector<std::string>::const_iterator iter = svec_old.begin();
iter != svec_old.end();
++iter )
{
if( GT4( *iter ))
{
std::cout << *iter << std::endl;
}
}
return 0;
}