Home > Software engineering >  Why am I experiencing an infinite loop here?
Why am I experiencing an infinite loop here?

Time:12-01

My code never stops asking for input so I think I must've made an infinite loop, but I can't find where the error is. I've also noticed that when inserting the input line by line, it prints the result of one loop after inserting the first line of the second loop, which seems incorrect to me. Please help me debug. (For further context, the code is supposed to receive a number we'll call n, and then scan 3n more lines, which are basically n bundles of 3 similar lines. The 2nd and 3rd lines are two words with the same num of characters, and the 1st line is that num. The output is whether or not these words are anagrams.)

#include <stdio.h>

int main() {
    int n, l;

    scanf("%d\n", &n);

    for (int i = 1; i <= n; i  ) {
        scanf("%d\n", &l);
        char A[l], B[l];

        for (int j = 0; j < l; j  ) {
            scanf("%c", &A[j]);
            scanf("\n");
        }
        for (int j = 0; j < l; j  ) {
            scanf("%c", &B[j]);
            scanf("\n");
        }

        for (int k = 0; k < l; k  ) {
            int result = 0;
            for (int j = 0; j < l; j  ) {
                if (A[k] == B[j]) {
                    result = 1;
                }
            }
            if (!result) {
                printf("\nNO\n");
                return 0;
            }
        }

        printf("\nYES\n");
    }
}

Example:

input:

2
6
listen
silent
4
Evil
live

output:

YES
NO

CodePudding user response:

It's because you are asking for input over and over and over again,

Not sure what you are trying to achieve, but start by removing the extra scanfs

try this,

#include <stdio.h>

int main() {
    int n, l;

    scanf("%d\n", &n);

    for (int i = 1; i <= n; i  ) {
        char A[l], B[l];


        for (int k = 0; k < l; k  ) {
            int result = 0;
            for (int j = 0; j < l; j  ) {
                if (A[k] == B[j]) {
                    result = 1;
                }
            }
            if (!result) {
                printf("\nNO\n");
                return 0;
            }
        }

        printf("\nYES\n");
    }
}

CodePudding user response:

Your scanf calls wait for an extra \n which requires more input to enter.

To fix this, remove the \n from your scanf calls. Also remove the extra calls when you enter A and B:

I have added some debug code to demonstrate where you are in your program execution while you enter your input.

#include <stdio.h>

int main() {
    int n, l;
    int res;

    res=scanf("%d", &n);

printf ("res=%d, n=%d\n", res, n);

    for (int i = 1; i <= n; i  ) {
        res = scanf("%d", &l);

printf ("res=%d, l=%d\n", res, l);

//        char A[l], B[l];
        char A[l 1], B[l 1];

        for (int j = 0; j < l; j  ) {
            scanf(" %c", &A[j]);
        }      

printf ("A done\n");

        for (int j = 0; j < l; j  ) {
            scanf(" %c", &B[j]);
        }

printf ("B done\n");

A[l] = 0; B[l] = 0;
printf ("A=\"%s\" - B=\"%s\"\n", A, B);
...

Now your input should work properly.

But your code also contains another error. You will treat "12344" and "11234" as correct match wchich is wrong.

To fix this you need to remove each matching character from B:

        for (int k = 0; k < l; k  ) {
            int result = 0;

// We compare A[k] with the remaining characters in B only
            for (int j = k; j < l; j  ) {
                if (A[k] == B[j]) {
                    result = 1;
                    B[j] = B[k]; // Replace matching character with non-matching character we checked earlier.
                    break;
                }
            }
            if (!result) {
                printf("\nNO\n");
                return 0;
            }
        }

        printf("\nYES\n");

This code stops as soon as the first match is found and removed that character from B. The entries in B are rearranged to keep the unused entries at the end.

The full code looks like this:

#include <stdio.h>

int main() {
    int n, l;
    int res;

    res=scanf("%d", &n);

printf ("res=%d, n=%d\n", res, n);

    for (int i = 1; i <= n; i  ) {
        res = scanf("%d", &l);

printf ("res=%d, l=%d\n", res, l);

//        char A[l], B[l];
        char A[l 1], B[l 1];

        for (int j = 0; j < l; j  ) {
            scanf(" %c", &A[j]);
        }      

printf ("A done\n");

        for (int j = 0; j < l; j  ) {
            scanf(" %c", &B[j]);
        }

printf ("B done\n");

A[l] = 0; B[l] = 0;
printf ("A=\"%s\" - B=\"%s\"\n", A, B);
        

        for (int k = 0; k < l; k  ) {
            int result = 0;
            for (int j = k; j < l; j  ) {
                if (A[k] == B[j]) {
                    result = 1;
                    B[j] = B[k];
                    break;
                }
            }
            if (!result) {
                printf("\nNO\n");
                return 0;
            }
        }

        printf("\nYES\n");
    }
}

output:

~/stackoverflow$ ./test
4
res=1, n=4
2
res=1, l=2
12
A done
21
B done
A="12" - B="21"

YES
6
res=1, l=6
liver1
A done
evil1r
B done
A="liver1" - B="evil1r"

YES
3
res=1, l=3
111
A done
111
B done
A="111" - B="111"

YES
4
res=1, l=4
abcd
A done
dcbb
B done
A="abcd" - B="dcbb"

NO
  • Related