【发布时间】:2017-09-28 02:55:03
【问题描述】:
因此,目标是检查 C 样式字符串是否以句号或感叹号结尾。但是,出于某种原因,我一直在说谎。
bool isItSentence(const char* s)
{
int x = strlen(s);
for (int c = 0; s[c] != '\0'; c++)
{
if (!isupper(s[0])) return false;
if (isupper(s[c]) && c > 0) return false;
if (s[x-1] != '.') return false;
if (s[x-1] != '!') return false;
}
return true;
}
int main()
{
std::string str = "Smelly.";
reverse(str.c_str());
std::cout << isItSentence(str.c_str()) << std::endl;
std::cout << strlen(str.c_str()) << std::endl;
system("pause");
这是我目前所拥有的。但是当我添加最后一个 if 语句来处理感叹号时,它返回零。有什么建议吗?
【问题讨论】:
标签: c++ arrays loops for-loop if-statement