看起來這應該很簡單,但我似乎無法弄清楚。我試圖編寫一個函數,如果位置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