Finding a character in a string in a vector
As the name suggests, I kind of made my program in a rather complicated way. BUT! Anyway, I might ask xD
This is a simple program I made in response to Accelerated C ++ Question 3-3, which is an amazing book in my opinion.
I created a vector:
vector<string> countEm;
This accepts all valid strings. So I have a vector containing the elements of the strings.
Then I created a function
int toLowerWords( vector<string> &vec )
{
for( int loop = 0; loop < vec.size(); loop++ )
transform( vec[loop].begin(), vec[loop].end(),
vec[loop].begin(), ::tolower );
which splits the input into all lowercase characters for easier counting. So far so good.
I created a third and final function to actually count the words and that's what I'm stuck with.
int counter( vector<string> &vec )
{
for( int loop = 0; loop < vec.size(); loop++ )
for( int secLoop = 0; secLoop < vec[loop].size(); secLoop++ )
{
if( vec[loop][secLoop] == ' ' )
It looks ridiculous. Using a two dimensional array to call the characters of the vector until I find the space. Funny. I don't think this is an elegant or even viable solution. If that were a viable solution, I would then step back from the space and copy all the characters I found in a separate vector and count them.
Then my question. How can I scatter a vector of strings into individual words so that I can count them? I thought about using strchr, but it didn't give me any insight.
Solution via Neil:
stringstream ss( input );
while( ss >> buffer )
countEm.push_back( buffer );
From this I could easily count the (repeated) words.
Then I made a solution through Wilhelm that I will post it after I overwrote it as I accidentally deleted this solution! Stupid of me, but I'll post it as soon as I write it again ^^
I want to thank all of you for your input! The solutions worked and I became a slightly better programmer. If I could vote for your stuff, then I would: P As soon as I can, I will! And thanks again!
a source to share
If words are always separated by a space, the simplest way to separate them is to use a string stream:
string words = .... // populat
istringstream is( words );
string word;
while( is >> word ) {
cout << "word is " << word << endl;
}
You want to write a function to do this, of course, and apply it to your strings. Or is it better not to store strings in allm, but to split into words at the initial input.
You can use std::istringstream
to extract words one by one and count them. But this solution consumes O (n) in cosmic complexity.
string text("So many words!");
size_t count = 0;
for( size_t pos(text.find_first_not_of(" \t\n"));
pos != string::npos;
pos = text.find_first_not_of(" \t\n", text.find_first_of(" \t\n", ++pos)) )
++count;
Perhaps not as short as Neil's solution, but does not take up space and extra allocation beyond what is already in use.
a source to share
Use a tokenizer like the one listed here in section 7.3 to split the strings in your vector into individual words (or rewrite it so that it just returns the number of tokens) and loop over your vector to count the total number of tokens you encounter.
a source to share