#include<stdio.h>

int main()
{
    char a[60][60],b[60][60],i=0,n1,m1,n2,m2;
    int x,y;
    int j=0;
    char c;
    int p=-1,q=-1,f=-1,g=-1;
    scanf("%d%d",&n1,&m1);



    for(i=1;i<=n1;i++)
    scanf(" %s",a[i]);

    scanf("%d%d",&n2,&m2);




    for(i=1;i<=n2;i++)
    scanf(" %s",b[i]);

    for(i=1;i<=n1;i++)
    {
           for(j=1;j<=m1;j++)
           {
                   if(a[i][j]=='1')
                   {
                         p=i;
                         q=j;  
                         break;                   
                   }

           }
            if(p>0 || q>0)
                   break;
    }


    for(i=1;i<=n2;i++)
    {
           for(j=1;j<=m2;j++)
           {
                   if(b[i][j]=='1')
                   {
                         f=i;
                         g=j;
                         break;                     
                   }
           }

            if(g!=-1 || f!=-1)
                   break;
    }

    printf("%d %d\n",f-p,g-q);


    return 0;
}

this is a code snippet. it is not taking i/p in scanf(%s) lines in the code. it is exiting after taking 2 strings only. but not for n1 and n2. please tell ehat is problem ?

This thread was marked as solved. Did you solve your own problem?

On a side note, is it really so hard to type "input"? I mean, it's got to be less awkward to type and is only two keystrokes longer.

Comments
will not do this type of mistake in future

@ nitin1

Instead of using "%s" format directive, why don't you use scanf(" %[^\n]",a[i]); that is "%[^\n]" format directive.

Or else, use "%[^\n]%*c" format directive. It will take away all the things that is present in the input buffer, otherwise the newline or form feed may eat away your subsequent format directive.

Edited 4 Years Ago by np complete

Instead of using "%s" format directive, why don't you use scanf(" %[^\n]",a[i]); that is "%[^\n]" format directive.

Or else, use "%[^\n]%*c" format directive. It will take away all the things that is present in the input buffer, otherwise the newline or form feed may eat away your subsequent format directive.

Why would this be a better solution for line input than fgets()? Hell, without a field width this suggestion isn't even better than gets(), with the exception that you can add a field width to make it safe while gets() is never safe. ;)

deceptikon, please throw more light on this. how fgets() is more better than what npcpmplete has said. i am eager to know that. thanks

how fgets() is more better than what npcpmplete has said.

It more clearly shows your intention (read a line of string data), avoids the overhead of scanf() because fgets() doesn't need to interpret a format string, and of course it's easier to miss the huge bug of not including a field width with scanf(). Another consideration is if the size of the array that will hold the string isn't a compile time entity, in which case you need to construct the format string at runtime in a very awkward manner:

char *buf;        /* Buffer with unknown compile time size */
size_t size;      /* Runtime size of buf */
char fmt[BUFSIZ];

sprintf(fmt, "%%%d[^\n]%%*c", size);

/* Now use scanf() after constructing a format string */
if (scanf(fmt, buf) != 1) {
    /* Handle an input error */
}

There's nothing really wrong with using scanf(), assuming you use it correctly, but it's kind of like using a wrench as a hammer. It works, but it's not the most obvious tool, and the more obvious tool will probably work better.

Edited 4 Years Ago by deceptikon

hey! what is that in line 5 ? i didn't get anything in that line sorry to say. please clearify

i didn't get anything in that line sorry to say.

Did you run it? Did you try printing fmt to see what its contents are after sprintf() runs? I don't mind explaining things, but it would be better for you to experiment first, because someone won't always be around to explain things to you.

This question has already been answered. Start a new discussion instead.