#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?
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.
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).
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.
scanf() just stops once it encounters a whitespace as it considers this variable "done".
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;
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With