【问题标题】:Why am I getting this error in a program that tries to count the alphabets of a string?为什么我在尝试计算字符串字母的程序中出现此错误?
【发布时间】:2018-02-05 16:54:22
【问题描述】:

调试断言失败! 程序: ...Laske aakkoset\Debug\Ohjelmontitehtävä 4.1 Laske aakkoset.exe 文件:minkernel\crts\ucrt\appcrt\convert\isctype.cpp

Line: 36 Expression: c >= -1 && c <= 255

我的代码:

#include <stdio.h>
#include <ctype.h>

int count_alpha(const char *str) {
    int i = 0;
    int j = 0;
    while (*str) {
        if (isalpha(str[j])) {
            i++;
            j++;
            str++;
        }
        else {
            i = i;
            j++;
            str++;
        }
    }
    printf("%d", i);
    return (0);

}

int main(void) {

    char lol[] = "asdf";
    count_alpha(lol);
}

【问题讨论】:

  • i = i; 有趣的做法
  • C 还是 C++?选择(您的文件具有 .cpp 扩展名,但您的代码使用 C 实践。)
  • 增加jstr,不能同时增加。
  • documentation 告诉我 isalpha 的行为是未定义的,除非它的输入可以用 unsigned 字符表示,断言条件也表明这是导致问题的原因.尝试static_cast&lt;unsigned char&gt; 以确保安全?
  • std::cout &lt;&lt; std::count_if(lol, lol + 4, isalpha); -- 如果你真的在使用 C++,这基本上就是整个代码。

标签: c++ c visual-studio


【解决方案1】:

你增加你的 char* 指针和索引;但你只需要做一个。它可以相当简单地编辑为:

while (str[j]) {
    if (isalpha(str[j]))
        i++;
    ++j;
}

while (*str) {
    if (isalpha(*str))
        i++;
    ++str;
}

尝试同时增加两者将导致奇数长度的未定义行为,因为您将开始读取尚未分配给程序的内存。

【讨论】:

    【解决方案2】:

    简化!这是一个有效的 C 程序。问题中没有任何内容暗示 C++。

    #include <stdio.h>
    #include <ctype.h>
    
    int count_alpha(const char *str) {
        int count = 0;
        while (*str) {
            if (isalpha(*str)) {
                ++count;
            }
            ++str;
        }
        printf("%d", count);
        return count;
    
    }
    
    int main(void) {
    
        char lol[] = "asdf";
        count_alpha(lol);
    }
    

    这是一个更接近 C++ 的版本。

    #include <string_view>
    #include <cctype>
    #include <iostream>
    int count_alpha(const std::string_view str) {
        int count = 0;
        for(auto c: str) {
            if (isalpha(*str)) {
                ++count;
            }
        }
        std::cout << count << std::endl;
        return count;
    }
    
    int main(void) {
        char lol[] = "asdf";
        count_alpha(lol);
    }
    

    这是另一个不使用 string_view 的 C++ 版本:

    #include <cctype>
    #include <iostream>
    #include <algorithm>
    
    template<class T>
    size_t count_alpha(const T &str) {
        size_t count = std::count_if(std::begin(str), std::end(str), std::isalpha);
        std::cout << count << std::endl;
        return count;
    }
    
    int main(void) {
        char lol[] = "asdf";
        count_alpha(lol);
    }
    

    【讨论】:

    • .cpp 扩展名除外。
    • @michael - 作业:使用 std::count_if 重写它。
    • 我试试你的代码,&lt;string_view&gt; 不起作用:string_view: No such file or directory
    • 很抱歉。这是一个新事物。我认为是 C++17。也许吧。
    • @michael Look Ma,没有 string_view。
    猜你喜欢
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2019-05-10
    • 2021-09-17
    • 2013-02-28
    • 1970-01-01
    相关资源
    最近更新 更多