检查 arr2 是否在 arr1 内的程序 - 无输出

program that check if arr2 is within arr1 - no output

我正在尝试编写一个程序来检查 arr2 是否在 arr1 内,然后打印结果。

我写了这个:

#include <stdio.h>
#define N 10

int main() {
    char arr1[10];
    char arr2[10];
    int i, j, number_of_arr1, number_of_arr2;
    
    printf("Please enter up to %d characters\n", N);
    
    for (i=0; i<N; ++i) {
        scanf(" %c", &arr1[i]);
        if (arr1[i] != 0) {
            ++number_of_arr1;
        }
    }

    
    printf("Please enter up to %d characters again\n", N);
    
    for (i=0; i<N; ++i) {
        scanf(" %c", &arr2[i]);
        if (arr2[i] != 0) {
            ++number_of_arr2;
        }
    }


    
  
    for (i = 0; j < number_of_arr2; ++j) {
        for (j = 0; arr2[j] != arr1[i]; ++i) {
            if (i == number_of_arr1) {
                printf("The second array is not within the first array");
                break;
            }
        }
        i = 0;
        
    }
    
    printf("The second array is within the first array");
    
    
    
    return 0;
}

当我输入例如 “asdfghjklq” 然后 “asd”或“w” 没有输出。

另外,如果第一次输入少于 10 个字符(例如“asd”),程序就会卡住。

问题是你的循环

for (i=0; i<N; ++i) {
    scanf(" %c", &arr2[i]);
    if (arr2[i] != 0) {
        ++number_of_arr2;
    }
}

将始终尝试准确读取 10 个非whitespace characters. It will continue attempting to read that many characters, even after the user presses the ENTER key. That explains why your program appears to be stuck and you get no output: Your program is still waiting for the user to enter 10 non-whitespace characters. This behavior is clearly visible when running your program line by line in a debugger

问题是函数 scanf 通常像对待任何其他白色 space 字符一样对待换行符。因此它不会将换行符视为与 space 字符有任何不同。那不是您想要的行为。您希望程序在 scanf 遇到换行符时立即跳出循环。

为了读取单行输入,我建议您使用函数 fgets 而不是 scanf,并将您的循环替换为以下代码:

char *p;

//attempt to read one line of input
if ( fgets( arr2, sizeof arr2, stdin ) == NULL )
{
    fprintf( stderr, "Input error!\n" );
    exit( EXIT_FAILURE );
}

//find the newline character
p = strchr( arr2, '\n' );
if ( p == NULL )
{
    //the reason why we did not find a newline character was
    //probably because the user input was too long to fit into
    //the array
    fprintf( stderr, "Too many characters!\n" );
    exit( EXIT_FAILURE );
}

//remove the newline character by overwriting it with a
//terminating null character
*p = '[=11=]';

//calculate length of string
number_of_arr2 = p - arr2;

请注意,您必须 #include <stdlib.h>#include <string.h> 才能使用此代码。

但是,这段代码有一个问题:函数 fgets 与 null-terminated 字符串一起使用,这意味着字符数组需要 space 不仅用于 10 个字符,而且也 space 用于终止空字符。此外,函数 fgets 也存储换行符(这很有用,这样您就可以看到是否读入了整行)。因此,10的数组大小是不够的,因为除了换行符和终止空字符外,它只能存储8个字符。如果您希望能够存储 10 个字符,则必须将数组 arr2 的大小增加到 12.

应用上述修复程序后,将上面的代码放入其自己的函数中,输入两个最多 10 个字符的字符串的代码应如下所示:

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

#define N 10

//NOTE: When using this function, buffer_size must have 2 bytes
//more than the actual string length, because it needs space for
//the newline character (which will be removed) and the
//terminating null character.
//This function will return the length of the string without the
//newline character.
int get_line_from_user( char *buffer, int buffer_size );

int main()
{
    char arr1[12];
    char arr2[12];
    int number_of_arr1, number_of_arr2;

    //read the first string and determine its length
    printf( "Please enter up to %d characters: ", N );
    number_of_arr1 = get_line_from_user( arr1, sizeof arr1 );

    //read the second string
    printf( "Please enter up to %d characters again: ", N );
    number_of_arr2 = get_line_from_user( arr2, sizeof arr2 );

    //print the input
    printf(
        "\n"
        "String 1:\n"
        "Length: %d\n"
        "Content: %s\n"
        "\n"
        "String 2:\n"
        "Length: %d\n"
        "Content: %s\n"
        "\n",
        number_of_arr1, arr1, number_of_arr2, arr2
    );
}

int get_line_from_user( char *buffer, int buffer_size )
{
    char *p;

    //attempt to read one line of input
    if ( fgets( buffer, buffer_size, stdin ) == NULL )
    {
        fprintf( stderr, "Input error!\n" );
        exit( EXIT_FAILURE );
    }

    //find the newline character
    p = strchr( buffer, '\n' );
    if ( p == NULL )
    {
        //the reason why we did not find a newline character was
        //probably because the user input was too long to fit into
        //the array
        fprintf( stderr, "Too many characters!\n" );
        exit( EXIT_FAILURE );
    }

    //remove the newline character by overwriting it with a
    //terminating null character
    *p = '[=12=]';

    //return the length of the string
    return p - buffer;
}

该程序具有以下行为:

Please enter up to 10 characters: 01234567890
Too many characters!
Please enter up to 10 characters: 0123456789
Please enter up to 10 characters again: 123

String 1:
Length: 10
Content: 0123456789

String 2:
Length: 3
Content: 123

如您所见,程序现在可以接受少于 10 个字符的输入。

我没有包含您程序中试图确定第二个字符串是否是第一个字符串的一部分的部分,因为您程序的那部分没有意义。您正在使用变量 j 的值,尽管这个值是不确定的,因为您没有初始化它。

由于该问题与您的问题中提到的问题(程序卡住)完全不同,因此我不会在我的回答中进一步讨论这个问题。但是,请随时就该问题提出新问题,但我建议您先阅读以下内容:

How to debug small programs?