【发布时间】:2014-01-17 09:53:30
【问题描述】:
我试图通过使用预定义函数isalpha()检查字符串foo中的每个字符来计算字符串中的字母数
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
string foo = "aaaaaaa1";
int count=0;
for (int i=0;i<foo.length();i++)
{
if ( isalpha(foo[i]) == true)
{
count++;
}
}
cout<<count;
system("PAUSE");
}
预期输出:
7
当前输出:
0
错误是function isalpha is not returning true for alphabetic,
有人可以向我解释为什么以及如何解决该问题以检查给定字符是否为字母
【问题讨论】:
-
与
true比较是一种反模式(至少在C 和C++ 中)。见stackoverflow.com/questions/356217/…