Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is scanf not allowing any more input?

Tags:

c

scanf

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

int main() {
    char a[100], b[100];
    char *ret;
    scanf("%[^\n]s", a);
    scanf("%[^\n]s", b);
    ret = strstr(a, b);
    if (ret != NULL)
        printf("its a substring");
    else
        printf("not a substring");
    return 0;
}

My aim was to check whether a substring is present in the parent string in the string or not. I learned about the strstr() function from here.

I have previously used %[^\n]s in my codes before and they worked well. But, in this case as soon as I hit return/enter after typing one string, the output is not a substring.

What is it that I am doing wrong?

like image 535
Karan Singh Avatar asked Jan 29 '17 12:01

Karan Singh


People also ask

Why is scanf not taking input in C?

You may've used the scanf inside a while loop or for loop or do while loop or if else statement or switch case statement or in a remote user defined function that doesn't satisfy the condition to enter into it. In that case that block will be skipped and scanf will not work.

Why scanf is not working?

The other answers are correct - %c does not skip whitespace. The easiest way to make it do so is to place whitespace before the %c : scanf(" %c", &achar); (Any whitespace in the format string will make scanf consume all consecutive whitespace).

Can scanf take multiple inputs?

Inputting Multiple ValuesIf you have multiple format specifiers within the string argument of scanf, you can input multiple values. All you need to do is to separate each format specifier with a DELIMITER - a string that separates variables.

Why scanf stops at space?

scanf() just stops once it encounters a whitespace as it considers this variable "done".


Video Answer


2 Answers

The first call to scanf() stops when it sees a newline ('\n') but it's still in the input stream. So, the second call fails immediately as it sees the (same) newline character.

You should always check the return value of scanf() calls for failure.

You can insert a getchar(); call in between the scanf() calls to consume the newline character. Or better use fgets() and handle the newline character.


This is how you could use fgets() in your code:

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

int main(void) {
     char a[100], b[100];
     char *ret;
     if (fgets(a, sizeof a, stdin) == NULL) {
        /* error handling */
     }

     a[strcspn(a, "\n")] = '\0';
     if (fgets(b, sizeof b, stdin) == NULL) {
       /* error handling */
     }

     b[strcspn(b, "\n")] = '\0';
     ret=strstr(a, b);
     if(ret!=NULL)
         printf("its a substring");
     else
         printf("not a substring");
     return 0; 
}
like image 157
P.P Avatar answered Oct 22 '22 22:10

P.P


The scanf() format you use %[^\n] does not have a trailing s and you should provide the maximum number of characters to store into the destination array. Since the array has a size of 100, you should specify %99[^\n].

Furthermore, you must skip the newline between the 2 %99[^\n] otherwise the second conversion will fail since there would no characters different from \n to match.

Here is a simple correction using scanf(). usr has a good alternative using fgets() which is a simpler way to parse input:

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

int main(void) {
    char a[100], b[100], c;

    if (scanf("%99[^\n]%c%99[^\n]", a, &c, b) == 3 && c == '\n') {
        if (strstr(a, b))
            printf("its a substring\n");
        else
            printf("not a substring\n");
    }
    return 0;
}
like image 33
chqrlie Avatar answered Oct 22 '22 21:10

chqrlie