I have the following C code that calculates the total number of words and stores the the value in count variable. So, the question is how can I store those particular words from a sentence in a variable so I can later use them to match the words in the sentence if I pass a word to match with.
Thanks for the help.
#include <iostream>
#include <cstring>
#include <new>
#include <cctype>
int wordsInString(const char* );
int main()
{
wordsInString("My name is Donnie");
return 0;
}
int wordsInString(const char* s)
{
int count = 0;
int len = strlen(s);
int i;
for(i=0;i<len;i )
{
while(i<len && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n'))
{
i ;
}
if(i<len)
{
count ;
while(i<len && (s[i] != ' ' && s[i] != '\t' && s[i] != '\n'))
{
i ;
}
}
}
std::cout << "The total count: " << count << std::endl;
return count;
}
CodePudding user response:
I can offer another way to solve this problem - using the algorithm Depth first search.
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
const int maximumSize=40;
string wordsInString="My name is Donnie";
vector<string> words;
string temporary;
vector<int> visitedWords(maximumSize, 0);
template<class Type>
void showContent(Type input)
{
for(int i=0; i<input.size(); i)
{
cout<<input[i]<<", ";
}
return;
}
void dfsWords(int current, int previous)
{
if(visitedWords[current]==1)
{
return;
}
visitedWords[current]=1;
if(isspace(wordsInString[current])==false)
{
temporary.push_back(wordsInString[current]);
}
else
{
words.push_back(temporary);
temporary.clear();
}
if(current==(wordsInString.size()-1))
{
words.push_back(temporary);
temporary.clear();
}
for(int next=current; next<wordsInString.size(); next)
{
dfsWords(next, current);
}
return;
}
void solve()
{
dfsWords(0, -1);
cout<<"wordsInString <- ";
showContent(wordsInString);
cout<<endl<<"words <- ";
showContent(words);
return;
}
int main()
{
solve();
return 0;
}
Here is the result:
wordsInString <- M, y, , n, a, m, e, , i, s, , D, o, n, n, i, e,
words <- My, name, is, Donnie,
CodePudding user response:
Perhaps I misunderstood the problem in my initial comments, but if you just want to count the number of space-delimited "words" in a string, then I recommend using an std::istringstream
for the string, then use a loop extracting the space-delimited "words" one by one using the normal input operator >>
into a dummy string variable, and increase a counter in the loop.
Perhaps something like this:
unsigned wordsInString(std::string const& string)
{
// A stream we can read words from
std::istringstream stream(string);
// The word counter
unsigned word_counter;
// A dummy string, the contents of the string will never be used
std::string dummy;
// While we can read words from the stream, increase counter
for (word_counter = 0; stream >> dummy; word_counter)
{
// Empty
}
// Return the counter
return word_counter;
}
This works because:
The result of the
>>
stream extract operator is the stream itself, and when converted to a boolean value it will be false when we reach the end of the stream, which breaks the loop; AndThe stream extraction operator separates on white-space (space, newline, tab, etc.); And
For each successful extraction of a word from the stream, we will increase the counter, so the loop counts the "words" in the stream.