One simple way is to use the ispunct()
and isspace()
functions on each character. A simple boolean keeps track of whether you have consecutive punctuation/space:
size_t CountWords(string input)
{
//A string with no punctuation/space is one word.
size_t output = 1;
char tempc = *input.rbegin();
if (ispunct(tempc) || isspace(tempc))
output = 0;
bool consecutive = false;
for (char c : input)
{
if ( (ispunct(c) || isspace(c)))
{
if (!consecutive)
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
If a custom delimiter list is required something like this would work:
size_t CountWords(string input, string delimiters)
{
size_t output = 1;
string::const_iterator index = delimiters.cbegin();
string::const_iterator end = delimiters.cend();
if (find(index, end, *input.rbegin()) != end)
output = 0;
bool consecutive = false;
for (char c : input)
{
if (find(index, end, c) != end)
{
if (!consecutive)
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
One simple way is to use the ispunct()
and isspace()
functions on each character. A simple boolean keeps track of whether you have consecutive punctuation/space:
size_t CountWords(string input)
{
//A string with no punctuation/space is one word.
size_t output = 1;
bool consecutive = false;
for (char c : input)
{
if ( (ispunct(c) || isspace(c)))
{
if (!consecutive)
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
If a custom delimiter list is required something like this would work:
size_t CountWords(string input, string delimiters)
{
size_t output = 1;
string::const_iterator index = delimiters.cbegin();
string::const_iterator end = delimiters.cend();
bool consecutive = false;
for (char c : input)
{
if (find(index, end, c) != end)
{
if (!consecutive)
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
One simple way is to use the ispunct()
and isspace()
functions on each character. A simple boolean keeps track of whether you have consecutive punctuation/space:
size_t CountWords(string input)
{
//A string with no punctuation/space is one word.
size_t output = 1;
char tempc = *input.rbegin();
if (ispunct(tempc) || isspace(tempc))
output = 0;
bool consecutive = false;
for (char c : input)
{
if ( (ispunct(c) || isspace(c)))
{
if (!consecutive)
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
If a custom delimiter list is required something like this would work:
size_t CountWords(string input, string delimiters)
{
size_t output = 1;
string::const_iterator index = delimiters.cbegin();
string::const_iterator end = delimiters.cend();
if (find(index, end, *input.rbegin()) != end)
output = 0;
bool consecutive = false;
for (char c : input)
{
if (find(index, end, c) != end)
{
if (!consecutive)
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
One simple way is to use the ispunct()
and isspace()
functions on each character. A simple boolean keeps track of whether you have consecutive punctuation/space:
size_t CountWords(string input)
{
//A string with no punctuation/space is one word.
size_t output = 1;
bool consecutive = false;
for (char c : input)
{
if (!consecutive && (ispunct(c) || isspace(c)))
{
if output++;(!consecutive)
consecutiveoutput++;
= true;
consecutive = }true;
}
else
consecutive = false;
}
return output;
}
If a custom delimiter list is required something like this would work:
size_t CountWords(string input, string delimiters)
{
size_t output = 1;
string::const_iterator index = delimiters.cbegin();
string::const_iterator end = delimiters.cend();
bool consecutive = false;
for (char c : input)
{
if (!consecutive && find(index, end, c) != end)
{
if output++;(!consecutive)
consecutiveoutput++;
= true;
consecutive = }true;
}
else
consecutive = false;
}
return output;
}
One simple way is to use the ispunct()
and isspace()
functions on each character. A simple boolean keeps track of whether you have consecutive punctuation/space:
size_t CountWords(string input)
{
//A string with no punctuation/space is one word.
size_t output = 1;
bool consecutive = false;
for (char c : input)
{
if (!consecutive && (ispunct(c) || isspace(c)))
{
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
If a custom delimiter list is required something like this would work:
size_t CountWords(string input, string delimiters)
{
size_t output = 1;
string::const_iterator index = delimiters.cbegin();
string::const_iterator end = delimiters.cend();
bool consecutive = false;
for (char c : input)
{
if (!consecutive && find(index, end, c) != end)
{
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
One simple way is to use the ispunct()
and isspace()
functions on each character. A simple boolean keeps track of whether you have consecutive punctuation/space:
size_t CountWords(string input)
{
//A string with no punctuation/space is one word.
size_t output = 1;
bool consecutive = false;
for (char c : input)
{
if ( (ispunct(c) || isspace(c)))
{
if (!consecutive)
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
If a custom delimiter list is required something like this would work:
size_t CountWords(string input, string delimiters)
{
size_t output = 1;
string::const_iterator index = delimiters.cbegin();
string::const_iterator end = delimiters.cend();
bool consecutive = false;
for (char c : input)
{
if (find(index, end, c) != end)
{
if (!consecutive)
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
One simple way is to use the ispunct()
and isspace()
functions on each character. A simple boolean keeps track of whether you have consecutive punctuation/space:
size_t CountWords(string input)
{
//A string with no punctuation/space is one word.
size_t output = 1;
bool consecutive = false;
for (char c : input)
{
if (!consecutive && (ispunct(c) || isspace(c)))
{
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
If a custom delimiter list is required something like this would work:
size_t CountWords(string input, string delimiters)
{
size_t output = 1;
string::const_iterator index = delimiters.cbegin();
string::const_iterator end = delimiters.cend();
bool consecutive = false;
for (char c : input)
{
if (!consecutive && find(index, end, c) != end)
{
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
One simple way is to use the ispunct()
and isspace()
functions on each character. A simple boolean keeps track of whether you have consecutive punctuation/space:
size_t CountWords(string input)
{
//A string with no punctuation/space is one word.
size_t output = 1;
bool consecutive = false;
for (char c : input)
{
if (!consecutive && (ispunct(c) || isspace(c)))
{
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
One simple way is to use the ispunct()
and isspace()
functions on each character. A simple boolean keeps track of whether you have consecutive punctuation/space:
size_t CountWords(string input)
{
//A string with no punctuation/space is one word.
size_t output = 1;
bool consecutive = false;
for (char c : input)
{
if (!consecutive && (ispunct(c) || isspace(c)))
{
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}
If a custom delimiter list is required something like this would work:
size_t CountWords(string input, string delimiters)
{
size_t output = 1;
string::const_iterator index = delimiters.cbegin();
string::const_iterator end = delimiters.cend();
bool consecutive = false;
for (char c : input)
{
if (!consecutive && find(index, end, c) != end)
{
output++;
consecutive = true;
}
else
consecutive = false;
}
return output;
}