H
H
HoShik2020-12-01 10:39:25
C++ / C#
HoShik, 2020-12-01 10:39:25

Why is it not reading the line?

I can't figure out why it doesn't read the line:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void main() 
{
    char c, String[50];
    int count = 0, i;
    puts("Input string: ");
    gets(String);
    puts("Input character: ");
    scanf("%s", &c);
    for(i=0; i<strlen(String); i++) 
    {
        if(String[i]==c) 
        {
           count++; 
        }
    }
    printf("Count of characters %s %d", c,count);
}

The loop seems to work, but why doesn't it compare?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Codebaker, 2020-12-01
@HoShik

scanf("%s", &c);
Read a string into a character. What is a storka? This is a null-terminated character sequence. Where does this zero go? That's right, to the beginning of String[50], zeroing out the string for you. How to fix? Use "%c"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question