我怎么能算一些函数ispunct没有的标点符号

时间:2015-12-20 09:31:50

标签: c++

      for (int i = 0; i < strlen(s); i++)
       if(ispunct(s[i]))

我需要编写函数,其中我必须计算标点符号,如下所示;但是ispunct功能并没有全部  。 , - ; :'“()......?!

我试图使用strchr,但我找不到“'” 以上这些是“仅”考虑过,我怎么能真正计算它们?

谢谢!

2 个答案:

答案 0 :(得分:2)

我只会列出您关心的所有字符,并使用find_first_of来确定字符是否在那里。像这样:

bool isPunctuation(char c)
{
   static const std::string punctuations(".,-;:'\"()?");
   if (punctuations.find_first_of(c) != std::string::npos)
   {
      return true;
   }
   return false;
}

答案 1 :(得分:1)

定义一个包含您要搜索的所有符号的字符串:

j = num // i

使用std::string::find测试输入字符串的单个字符(如果它是std::string searchPunct=".,-;:'\"()?!" 的成员):

searchPunct