Home > Enterprise >  program that check if arr2 is within arr1 - no output
program that check if arr2 is within arr1 - no output

Time:06-03

Im trying to make a program that check if arr2 is within arr1 and than print the result.

I wrote this:

#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;
}

when I input for example "asdfghjklq" and than "asd" or "w" there is no output.

and another thing, if put less than 10 characters in the first time ("asd" for example), the program just stuck.

CodePudding user response:

The problem is that your loop

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

will always attempt to read exactly 10 non-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.

The problem is that the function scanf generally treats the newline character like any other whitespace character. So it does not treat a newline character any different than a space character. That is not the behavior that you want. You want your program to break out of the loop as soon as scanf encounters a newline character.

In order to read a single line of input, I recommend that you use the function fgets instead of scanf, and replace your loop with the following code:

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 = '\0';

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

Note that you will have to #include <stdlib.h> and #include <string.h> for this code to work.

However, this code has one issue: The function fgets works with null-terminated strings, which means that it the character array requires space not just for the 10 characters, but also space for the terminating null character. Also, the function fgets stores the newline character too (which is useful, so that you can see whether a complete line was read in). Therefore, an array size of 10 is not sufficient, because it can only store 8 characters in addition to the newline character and the terminating null character. If you want to be able to store 10 characters, you will have to increase the size of the array arr2 to 12.

After applying the fixes mentioned above putting the code above into its own function, your code to input two strings of up to 10 characters should look like this:

#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 = '\0';

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

This program has the following behavior:

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

As you can see, the program is now able to accept less than 10 characters of input.

I did not include the part of your program which attempts to determine whether the second string is part of the first string, because that part of your program does not make sense. You are using the value of the variable j, although this value is indeterminate, because you did not initialize it.

Since that issue is a completely different issue than the issue mentioned in your question (program getting stuck), I will not cover this issue any further in my answer. However, feel free to ask a new question on that issue, but I suggest that you read this first:

How to debug small programs?

  • Related