Unique Lines and Words? How to implement it?
I'm having trouble with this program. The program is supposed to tell the
user the number of lines, words, characters, unique lines, and unique
words there are in a given input. So far, words and characters are okay.
However, if the user wants to input more than one line, how do I do that?
The functions will only output the results of one line at a time, rather
than adding the results of both lines together. Also, I can't get the
Unique Lines and Unique Words to work properly. I just got into C++ so I
don't really have much experience. Can someone please help me?
Problems:
Program reads one line at a time, so when the user inputs multiple times,
the program produces the results separately rather than adding it together
as one entity.
Unique Lines and Unique Words are not working. Any ideas how to implement
it using the library used in the program.
Blockquote
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <set>
using std::set;
// write this function to help you out with the computation.
unsigned long countLines()
{
return 1;
}
unsigned long countWords(const string& s)
{
int nw =1;
for (size_t i = 0; i < s.size(); i++)
{
if (s[i] == ' ') //everytime the function encounters a
whitespace, count increases by 1)//
{
nw++;
}
}
return nw;
}
unsigned long countChars(const string& s)
{
int nc = 0;
for (size_t i = 0; i < s.size(); i++)
{
if ( s[i] != ' ') //everytime the function encounters a character
other than a whitespace, count increases//
{
nc++;
}
}
return nc;
}
unsigned long countUnLines(const string& s, set<string>& wl)
{
/*CAT\nFOX
CAT_\n_CAT
m1 m2 m1 m2
Check for a \n character. From [h][e][l][l][o][\n][w][o][r][l][d][\0]
0 1 2 3 4 5 6 7 8 9 10 11
int m1 = 0;
int m2 = 0;
string substring;
for(m2 = 0; m2 <= s.size(); m2++){
if (m2 == '\n' || m2 == '\0'){
substring = s.substr(m1,m2);
wl.insert(substring);
m1 = m2 + 2;}
}
return wl.size();
int unl = 0;
wl.insert(s);
unl++;
return unl;
}
unsigned long countUnWords(const string& s, set<string>& wl)
{
int m1 = 0;
int m2 = 0;
string substring;
for(m2 = 0; m2 <= s.size(); m2++){
if (m2 != ' ' )
substring = s.substr(m1,m2);
wl.insert(substring);
m1 = m2 + 2;}
}
return wl.size();
int unw = 0;
wl.insert(s);
unw++;
return unw;
}
int main()
{
//stores string
string s;
//stores stats
unsigned long Lines = 0;
unsigned long Words = 0;
unsigned long Chars = 0;
unsigned long ULines = 0;
unsigned long UWords = 0;
//delcare sets
set<string> wl;
while(getline(cin,s))
{
Lines += countLines();
Words += countWords(s);
Chars += countChars(s);
ULines += countUnLines(s,wl);
UWords += countUnWords(s);
cout << Lines << endl;
cout << Words<< endl;
cout << Chars << endl;
cout << ULines << endl;
cout << UWords << endl;
Lines = 0;
Words = 0;
Chars = 0;
ULines = 0;
UWords = 0;
}
return 0;
}
No comments:
Post a Comment