for (int i = 0; i < strlen(s); i++)
if(ispunct(s[i]))
I need to write function in which i have to count punctuation marks, as follow; but ispunct function doesn't have them all . , - ; : ' " ( ) ... ? !
I tried to use the strchr, but I couldn't find the " ' " These from above are "only" considered, how I could actually count them ??
Thanks!
I would just list all the characters you care about, and use find_first_of to identify if the character is there. Something like this:
bool isPunctuation(char c)
{
static const std::string punctuations(".,-;:'\"()?");
if (punctuations.find_first_of(c) != std::string::npos)
{
return true;
}
return false;
}
Define a string with all the signs you want to search for:
std::string searchPunct=".,-;:'\"()?!"
Test eache single character of the of the input string, if it is member of searchPunct, by using std::string::find:
size_t countPunct( const char *s )
{
static const std::string searchPunct = ".,-;:'\"()?!";
size_t count = 0;
for ( int i = 0; i < strlen( s ); i++ )
{
if ( searchPunct.find( s[i] ) != string::npos )
count ++;
}
return count;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With