简体   繁体   English

为什么我的程序计算字母和数字没有运行?

[英]Why is my program counting letters and digits not running?

I wrote this function tat checks for upper, lower and digits in a string but when I'm trying to run the code this pops up and cant seem to understand the problem.我编写了这个函数来检查字符串中的大写、小写和数字,但是当我尝试运行代码时,它会弹出并且似乎无法理解问题。

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

#define size 50

void statistics(char str[], int *lower, int *upper, int *digits) {
    for (int i = 0; i < size; i++) {
        if (islower(str[i]) != 0) {
            *lower = *lower + 1;
        } else
        if (isupper(str[i]) != 0) {
            *upper = *upper + 1;
        } else
        if (isalpha(str[i])) {
            *digits = *digits + 1;
        }
    }
}

int main() {
    char str[size] = { " " };
    int upper = 0, lower = 0, digits = 0;

    printf("Enter a string:\n");
    gets_s(str);

    statistics(&str[size], &lower, &upper, &digits);
    printf("Lower: %d\nUpper: %d\nDigits %d", lower, upper, digits);

    return 0;
}

在此处输入图像描述

There are multiple problems in your code:您的代码中有多个问题:

  • the gets_s() function is not portable: it is optional and not supported on many systems. gets_s()函数不可移植:它是可选的,在许多系统上不受支持。 You forget to pass the array size, hence causing undefined behavior.您忘记传递数组大小,从而导致未定义的行为。 The compiler should output a diagnostic that you should not ignore.编译器应该输出一个你不应该忽略的诊断。 You should use fgets() instead.您应该改用fgets()

  • you should not pass char values to the isupper() and similar functions because they are only defined for values of the type unsigned char and the special negative value EOF .您不应将char值传递给isupper()和类似函数,因为它们仅针对unsigned char类型的值和特殊的负值EOF定义。 Use an unsigned char variable or cast the str[i] argument as (unsigned char)str[i] .使用unsigned char变量或将str[i]参数转换为(unsigned char)str[i]

  • you pass the address of the end of the char array instead of the beginning.您传递 char 数组末尾的地址而不是开头。 Just pass str as the argument to statistics .只需将str作为参数传递给statistics The statistics function reads characters beyond the end of the array, invoking undefined behavior, and one of these bytes happens to be a negative char value less than -1 triggering the diagnostic in your Visual C++ compiler runtime. statistics函数读取数组末尾以外的字符,调用未定义的行为,其中一个字节恰好是小于-1的负char值,从而触发 Visual C++ 编译器运行时中的诊断。 The error message is difficult to interpret, the IDE should point you to the calling code.错误消息很难解释,IDE 应该将您指向调用代码。

  • you iterate on the whole array, beyond the null terminator.您迭代整个数组,超出空终止符。 The contents of the array is undefined beyond the null terminator set by gets_s() or fgets() .数组的内容在gets_s()fgets()设置的空终止符之外未定义。 Just stop at the null terminator.只需停在空终止符处。

  • you test if (isalpha(ch)) where you probably mean to use if (isdigit(ch))你测试if (isalpha(ch))你可能打算使用if (isdigit(ch))

  • the isxxx functions return a non zero value for true and zero for false. isxxx函数为真返回非零值,为假返回零。 It is idiomatic in C to just write if (isdigit(c)) instead of if (isdigit(c) != 0) which seems redundant.在 C 中,只写if (isdigit(c))而不是if (isdigit(c) != 0)似乎是多余的,这是惯用的。

  • defining size as a macro is error prone.size定义为宏很容易出错。 Use upper case and a more explicit name.使用大写和更明确的名称。

Here is a modified version:这是修改后的版本:

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

#define LINE_SIZE 50

void statistics(const char *str, int *lower, int *upper, int *digits) {
    while (*str != '\0') {
        unsigned char ch = *str++;
        if (islower(ch)) {
            *lower += 1;
        } else
        if (isupper(ch)) {
            *upper += 1;
        } else
        if (isdigit(ch) {
            *digits += 1;
        }
    }
}

int main() {
    char str[LINE_SIZE];
    int upper = 0, lower = 0, digits = 0;

    printf("Enter a string:\n");
    if (fgets(str, sizeof str, stdin)) {
        statistics(str, &lower, &upper, &digits);
        printf("Lower: %d\nUpper: %d\nDigits %d", lower, upper, digits);
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的代码只计算字符串中小写字母的频率 - Why is my code only counting the frequency of lower case letters from my string 为什么我的程序无法计算字符串中的单词? - Why does my program not work for counting the words in a string? 为什么我的 C 程序计数不正确? 纸牌游戏 - Why is my C program not counting correctly? Card game 为什么我的 C 程序中途停止运行? - Why my C program stops running in the middle? 为什么我的 C 语言 GCD 程序没有运行? - Why is my GCD program in C not running? 为什么我的程序同时运行 if 和 else 条件? - why is my program running both the if and else conditions? C 程序添加单个数字、大小写字母和其他字符 - C program add individual digits, lower and uppercase letters, and other characters 统计C程序中字母、数字和特殊字符的个数 - Count the number of letters, digits and special characters in C program 试图弄清楚为什么我的 C 程序只抓住了我重复的数字之一 - Trying to figure out why my C program is only grabbing one of my repeated digits 为什么我的C程序无法正确计算和打印文本文件中的整数? - Why is my C program not counting and printing integers from a text file properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM