So i am extremely new to this. I have an assignment to count the number of lines, words, characters, unique lines and unique words from user input. So far I have gotten lines, unique lines and characters from my code. I thought I got the words but then it doesn't work when i factor in double spaces and tabs. Also i have no clue how to find the unique words. Please offer your assistance.
Code:
// What I dont have:
//words
//Total words
#include <iostream>
#include <string>
#include <set>
using namespace std;
unsigned long countWords(const string& s, set<string>& wl); //total words
int main()
{
int linenum=0, charnum=0, totalwords=0;
set<string> lines;
string input;
set<string> unique; //to store unique words from countWords function
while (getline(cin,input))
{
lines.insert(input);
linenum++;
charnum+= input.length();
totalwords += countWords(input,unique);
}
cout << linenum <<" "<< totalwords <<" "<< charnum <<" " << lines.size()<<" " << unique.size()<< endl;
system("PAUSE");
return 0;
}
unsigned long countWords(const string& s, set<string>& wl) //total words
{
int wcount=1;
for (unsigned int i=0; i < s.length(); i++)
{
if ((s.at(i) == ' ')&&(s.at(i)+1 !='\0')) {
wcount++;
}
}
return wcount;
}
Here is an example of how the function could look
#include <iostream>
#include <sstream>
#include <set>
#include <string>
#include <iterator>
#include <algorithm>
unsigned long countWords( std::set<string> &wl, const std::string &s )
{
std::istringstream is( s );
wl.insert( std::istream_iterator<std::string>( is ),
std::istream_iterator<std::string>() );
is.clear();
is.str( s );
return ( std::distance( std::istream_iterator<std::string>( is ),
std::istream_iterator<std::string>() ) );
}
//...
In this example puctuations are considered as parts of words.
If you do not know yet std::istringstream and other facilities of C++ then you can write the function the following way
#include <iostream>
#include <set>
#include <string>
unsigned long countWords( std::set<string> &wl, const std::string &s )
{
const char *white_space = " \t";
unsigned long count = 0;
for ( std::string::size_type pos = 0, n = 0;
( pos = s.find_first_not_of( white_space, pos ) ) != std::string::npos;
pos = n == std::string::npos ? s.size() : n )
{
++count;
n = s.find_first_of( white_space, pos );
wl.insert( s.substr( pos, ( n == std::string::npos ? std::string::npos : n - pos ) ) );
}
return count;
}
//...
you need to put +1 inside brackets,your function will be like that
unsigned long countWords(const string& s, set<string>& wl) //total words
{
int wcount=0;// initial value must be zero
int N = 0;// you need to add this to count the characters of each word.
for (unsigned int i=0; i < s.length(); i++)
{
if ((s.at(i) == ' ')||(s.at(i+1) =='\0')) {// Condition must be or instead of and
wl.insert(s.substr(i-N-1,N));
++wcount;
N = 0;
}else ++N;
}
return wcount;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.