简体   繁体   English

输出文件 .exe 没有响应并停止在 C 中工作

[英]output file .exe is not responding and stopped working in C

It is a simple program to check whether string is palindrome or not.这是一个检查字符串是否为回文的简单程序。 I made following code in the program.我在程序中做了以下代码。

检查字符串是否为回文的程序

When i compile it, there is no error but when i try to run the .exe file, i always get following message.当我编译它时,没有错误,但是当我尝试运行 .exe 文件时,我总是收到以下消息。

.exe 没有响应

The fundamental problem here is that the loop exit condition can never be met, and this leads to an infinite loop:这里的根本问题是永远无法满足循环退出条件,这导致了无限循环:

(i != j || i != j - 1)

This condition is logically equivalent to:此条件在逻辑上等同于:

!(i == j && i == j - 1)

which is obviously always true, so the loop continues indefinitely.这显然总是正确的,所以循环无限期地继续下去。 The loop only needs to continue so long as j > i .只要j > i ,循环就需要继续。

There is another problem here;这里还有一个问题; namely that the dangerous function gets() should never be used.永远不应该使用危险函数gets() This function was deprecated in C99 and completely removed from the language in C11.此函数在 C99 中已弃用,并从 C11 中的语言中完全删除。 One alternative is to use fgets() instead.一种替代方法是改用fgets() Note that this function keeps the newline (if there is room in the buffer), so you will need to remove this after getting the input.请注意,此函数保留换行符(如果缓冲区中有空间),因此您需要在获取输入后将其删除。 Also, characters may be left in the input stream if the buffer is too small.此外,如果缓冲区太小,字符可能会留在输入流中。 For this reason, it would be better to declare a generously sized input buffer to reduce the risk of problems here.出于这个原因,最好声明一个大尺寸的输入缓冲区以减少这里出现问题的风险。 There is no reason not to use an input buffer holding 1000 characters, and I usually just use 4096 for something like this.没有理由不使用包含 1000 个字符的输入缓冲区,我通常只使用 4096 来处理这样的事情。 Memory is cheap.内存便宜。

Further, there is a risk of undefined behavior in the posted code, since the input string may be empty.此外,发布的代码中存在未定义行为的风险,因为输入字符串可能为空。 In this case, strlen(str) would be 0, so in the first execution of the loop body j would be decremented to -1.在这种情况下, strlen(str)将为 0,因此在循环体的第一次执行中, j将递减为 -1。 But the array access str[-1] is out of bounds, and leads to undefined behavior.但是数组访问str[-1]越界,并导致未定义的行为。

This problem can be fixed by checking that j is positive before the first decrement.这个问题可以通过在第一次减量之前检查j是否为正来解决。 Note that size_t is the correct type for array indices, as it is an unsigned integer type that is guaranteed to be able to hold any array index.请注意, size_t是数组索引的正确类型,因为它是一个unsigned整数类型,保证能够保存任何数组索引。 Also note that the strlen() function returns a value of type size_t , not int .另请注意, strlen()函数返回一个size_t类型的值,而不是int

Here is a modified version of the posted code.这是已发布代码的修改版本。 The size_t type is used for array indices. size_t类型用于数组索引。 The length of the input string is stored in j , which is then decremented only if it is a positive value;输入字符串的长度存储在j ,只有当它是正值时才会递减; this sets j to the index of the character preceding the null terminator so long as the input string is not an empty string.只要输入字符串不是空字符串,这会将j设置为空终止符之前的字符的索引。 The loop continues while j is greater than i and the characters indexed by these values match.j大于i并且由这些值索引的字符匹配时,循环继续。 After the loop terminates, str[i] and str[j] should agree;循环结束后, str[i]str[j]应该一致; if they do not, then the input was not a palindrome.如果没有,则输入不是回文。

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

#define BUF_SZ  4096

int main(void)
{
    char str[BUF_SZ];

    printf("Enter string:\n");
    fgets(str, sizeof str, stdin);           // Never use gets()
    str[strcspn(str, "\r\n")] = '\0';        // remove '\n'

    size_t i = 0;
    size_t j = strlen(str);
    if (j > 0) {
        --j;
    }

    while (i < j && str[i] == str[j]) {
        ++i;
        --j;
    }

    if (str[i] == str[j]) {
        puts("string is palindrome!!");
    } else {
        puts("string is not palindrome!!");
    }

    return 0;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM