isalpha 在 C++ 中导致 "Debug Assertion Failed"
isalpha causing a "Debug Assertion Failed" in c++
我有一个简短的程序,旨在通过首先测试数组中的字符是否为字母字符(跳过任何白色 space 或标点符号)来计算字符串中辅音的数量。我的 "if (isalpha(strChar))" 行代码一直收到调试断言失败。
"strChar"是一个变量,在for循环中被赋值为char值
抱歉,如果这是一个补救性问题,但我不确定哪里出错了。在此先感谢您的帮助!
#include <iostream>
#include <cctype>
using namespace std;
int ConsCount(char *string, const int size);
int main()
{
const int SIZE = 81; //Size of array
char iString[SIZE]; //variable to store the user inputted string
cout << "Please enter a string of no more than " << SIZE - 1 << " characters:" << endl;
cin.getline(iString, SIZE);
cout << "The total number of consonants is " << ConsCount(iString, SIZE) << endl;
}
int ConsCount(char *string, const int size)
{
int count;
int totalCons = 0;
char strChar;
for (count = 0; count < size; count++)
{
strChar = string[count];
if (isalpha(strChar))
if (toupper(strChar) != 'A' || toupper(strChar) != 'E' || toupper(strChar) != 'I' || toupper(strChar) != 'O' || toupper(strChar) != 'U')
totalCons++;
}
return totalCons;
}
我想问题是您总是循环遍历 81 个字符,即使输入的字符较少。这导致一些随机数据被馈送到 isalpha()
。
无论如何,我会更改代码以使用 std::string
而不是 char iString[SIZE]
来获取输入文本的实际长度。
函数ConsCount(char* string, const int size)
应该是这样的:
int ConsCount(char *string, const int size)
{
int consCount = 0;
char* begin = string;
for (char* itr = begin; *itr != '[=10=]'; ++itr)
{
if (isalpha(*itr)) {
char ch = toupper(*itr);
switch(ch) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
break; // If ch is any of 'A', 'E', 'I', 'O', 'U'
default:
++consCount;
}
}
}
return consCount;
}
如您所见,我将 if 语句替换为 switch 以提高可读性,并使用 char*
作为迭代器来迭代字符串。您可以删除代码中未使用的参数 int size
。
另外,我建议您使用 std::string
作为 safe-code。它还为您提供 iterator
class 来遍历 std::string
.
int ConsCount(char *string, const int size)
{
int consCount = 0;
char* begin = string;
for (char* itr = begin; *itr != '[=10=]'; ++itr)
{
if (isalpha(*itr)) {
char ch = toupper(*itr);
switch(ch) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
break; // If ch is any of 'A', 'E', 'I', 'O', 'U'
default:
++consCount;
}
}
}
return consCount;
try this
}
我有一个简短的程序,旨在通过首先测试数组中的字符是否为字母字符(跳过任何白色 space 或标点符号)来计算字符串中辅音的数量。我的 "if (isalpha(strChar))" 行代码一直收到调试断言失败。
"strChar"是一个变量,在for循环中被赋值为char值
抱歉,如果这是一个补救性问题,但我不确定哪里出错了。在此先感谢您的帮助!
#include <iostream>
#include <cctype>
using namespace std;
int ConsCount(char *string, const int size);
int main()
{
const int SIZE = 81; //Size of array
char iString[SIZE]; //variable to store the user inputted string
cout << "Please enter a string of no more than " << SIZE - 1 << " characters:" << endl;
cin.getline(iString, SIZE);
cout << "The total number of consonants is " << ConsCount(iString, SIZE) << endl;
}
int ConsCount(char *string, const int size)
{
int count;
int totalCons = 0;
char strChar;
for (count = 0; count < size; count++)
{
strChar = string[count];
if (isalpha(strChar))
if (toupper(strChar) != 'A' || toupper(strChar) != 'E' || toupper(strChar) != 'I' || toupper(strChar) != 'O' || toupper(strChar) != 'U')
totalCons++;
}
return totalCons;
}
我想问题是您总是循环遍历 81 个字符,即使输入的字符较少。这导致一些随机数据被馈送到 isalpha()
。
无论如何,我会更改代码以使用 std::string
而不是 char iString[SIZE]
来获取输入文本的实际长度。
函数ConsCount(char* string, const int size)
应该是这样的:
int ConsCount(char *string, const int size)
{
int consCount = 0;
char* begin = string;
for (char* itr = begin; *itr != '[=10=]'; ++itr)
{
if (isalpha(*itr)) {
char ch = toupper(*itr);
switch(ch) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
break; // If ch is any of 'A', 'E', 'I', 'O', 'U'
default:
++consCount;
}
}
}
return consCount;
}
如您所见,我将 if 语句替换为 switch 以提高可读性,并使用 char*
作为迭代器来迭代字符串。您可以删除代码中未使用的参数 int size
。
另外,我建议您使用 std::string
作为 safe-code。它还为您提供 iterator
class 来遍历 std::string
.
int ConsCount(char *string, const int size)
{
int consCount = 0;
char* begin = string;
for (char* itr = begin; *itr != '[=10=]'; ++itr)
{
if (isalpha(*itr)) {
char ch = toupper(*itr);
switch(ch) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
break; // If ch is any of 'A', 'E', 'I', 'O', 'U'
default:
++consCount;
}
}
}
return consCount;
try this
}