看起来这应该很简单,但我似乎无法弄清楚。我试图编写一个函数,如果位置pos处的字符是一个单词的第一个字符,它将返回true。一句话它是这种情况被定义为任何字母数字字符串。C++如何确定char是否是一个字中的第一个字符
这里是我的最新尝试:
bool wordBeginsAt (const std::string& message, int pos)
{
string temp;
int x;
for (x=pos;isAlphanumeric(message[x]==true);x++)
{
temp[x] = message[x];
}
if (temp[pos]!=0)
{
return false;
}
else
return true;
}
bool isAlphanumeric (char c)
{
return (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| (c >= '0' && c <= '9');
}
如果您只是对'pos'位置感兴趣,为什么您需要for循环?此外,条件'isAlphanumeric(message [x] == true)'看起来非常错误... – John3136 2015-02-05 22:35:12
'返回std :: isalnum(消息[pos])&&(pos == 0 ||!std :: isalnum(message [pos-1]));' – 2015-02-05 22:36:24
这似乎是一个死胡同,我试图使用string.find(“”)和string.substr()现在....我不能相信这是功能正在退缩。 – bryan 2015-02-05 22:39:48