-1
#include<stdio.h>

void main()
{

    int t,i=0;
    scanf("%d",&t);
    
    while(t--)
    {
        char c;
        scanf("%c",&c);
        char s[10]="codeforces";
        
        while(s[i]!='\0')
        {
            if(s[i]==c)
            printf("YES\n");
            
            else
            printf("NO\n");
            
            i++;
        }
    }

}

I tried 10 test cases but the output is 10 times NO

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • [Problems with `scanf`](https://stackoverflow.com/questions/20139415/). – Corvus Feb 03 '23 at 15:04
  • 1
    Using `" %c"` will help a bit. (But there are other issues, too.) – Steve Summit Feb 03 '23 at 15:05
  • 3
    There are rather *a lot* of problems with this code. Whatever learning resource you are using, get rid of it, and get a proper book on C. – Konrad Rudolph Feb 03 '23 at 15:05
  • 1
    `s[i]` may never be `\0`, because you didn't allocate enough space for it. Please use `char s[] = "codeforces";`. – Steve Summit Feb 03 '23 at 15:05
  • 1
    In code like this, there's little reason to read input one character at a time. And even when you *do* want to read one character at a time, `scanf` and `"%c"` are usually an unnecessarily frustrating and Byzantine way to do it. I suggest reading the entered password using `fgets`, and then using `strcmp` to test it. (But beware that `fgets` will leave the `\n` in the string, so you'll have to deal with it.) – Steve Summit Feb 03 '23 at 15:08
  • It looks like you want to print YES or NO once for each test case, but you're printing NO 10 times for the first test case, and no times for the other test cases because `i` is not reset. – Paul Hankin Feb 03 '23 at 15:08
  • 1
    'codeforces' is 10 characters. Where's room for the NUL? – tadman Feb 03 '23 at 15:09
  • 1
    The characterization "it is not taking input after I enter t" seems unlikely to be accurate unless you enter 0 for `t`. I guess that's your *interpretation*, but since it doesn't make sense with respect to the code, you should perhaps take a step back and review your actual observations. – John Bollinger Feb 03 '23 at 15:09
  • Have you tried entering values other than 10 for `t`? Does the program actually run to completion in your tests? – John Bollinger Feb 03 '23 at 15:17

2 Answers2

0

For starters according to the C Standard the function main without parameters shall be declared like

int main( void )

After this call of scanf

scanf("%d",&t);

the input buffer stores the new line character '\n' that corresponds to the pressed key Enter.

So the next call of scanf

scanf("%c",&c);

automatically reads this new line character '\n'.

To skip white space characters in the input buffer you need to place a leading space character in the format string like

scanf(" %c",&c);
      ^^^^

Also the inner while loop should be interrupted as soon as the inputted character is found in the string. The messages should be outputted after the while loop. And the variable i is not set to 0 within the outer while loop. So the code has a logical array. You should declare variables in minimum scopes where they are used.

Remove the declaration of i in this line

int t,i=0;

And rewrite the while loop the following way

    size_t i = 0;
    while ( s[i] !='\0' && s[i] != c ) ++i;

    if ( s[i] != '\0' )
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }    

In whole the program can look for example the following way.

#include <stdio.h>

int main( void )
{
    unsigned int t;

    if ( scanf( "%u", &t ) == 1 )
    {
        while ( t-- )
        {
            const char *s = "codeforces";
            char c;

            scanf( " %c", &c );
        
            size_t i = 0;
        
            while ( s[i] != '\0' && s[i] != c ) ++i;

            if ( s[i] != '\0' )
            {
                puts( "YES" );
            }
            else
            {
                puts( "NO" );
            }    
        }
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-3
#include<stdio.h>

void main()
{

    int t,i=0;
    scanf("%d",&t);
    
    while(t--)
    {
        char c;
        scanf(" %c",&c);  // note the addition of a space character before the %c format specifier
        char s[10]="codeforces";
        
        while(s[i]!='\0')
        {
            if(s[i]==c)
            printf("YES\n");
            
            else
            printf("NO\n");
            
            i++;
        }
    }
}

check this one

starball
  • 20,030
  • 7
  • 43
  • 238
DevJun
  • 1
  • 2