Require Help on Problem Code: PLAYSTR

,

Hi,

For the given problem Problem Code: PLAYSTR (link : CodeChef) my code is not taking input for a specific test case.

Code :

#include <stdio.h>
void check(char *s,char *r,int n) {
    int s1 = 0,s0 = 0,r1 = 0,r0 = 0;
    for(int i = 0;i < n;i++) {
        if(*(s+i) == '1')
            s1++;
        else
            s0++;
        if(*(r+i) == '1')
            r1++;
        else
            r0++;
    }
    if((s1 == r1) && (s0 == r0))
        printf("YES\n");
    else
        printf("NO\n");
}


int main(void) {
	// your code goes here
	int t,n;
	scanf("%d",&t);
	while(t-- > 0) {
	    scanf("%d",&n);
	    char s[n];
	    char r[n];
	    scanf("%s",s);
	    scanf("%s",r);
	    printf("%s\n %s\n",s,r);
	    check(s,r,n);
	    scanf("\n");
	}
	return 0;
}

Input Test cases :
3
5
11000
01001
3
110
001

(the problem test case)
16
1111100000110100
0011000011111000

Output :
11000
01001
YES
110
001
NO

0011000011111000
YES

Here the first string is not being displayed.

I tried with another test case but the input are working for those. Can you please help me point where it could be an issue?

Whenever we initialize a char array we should always have the size of this array to be one more than the number of characters that we will be storing in the array, this is done so that the null character which signifies end of the string is also able to be stored(’\0’). If you set size as n+1 that is char s[n+1], char r[n+1], then you will be able to correctly read and display your strings.
Hope it helps!
Thanks:)

Thanks Mr Buffet will keep this in mind going forward. Good to see you coding as well.

1 Like

It helps me make better investment decisions.

1 Like