Now i read somehwere:
The scanf() conversion %[\n] will match a newline character, while %[^\n] will match all characters up to a newline.
But in the following code:
#include<stdio.h>
int main() {
printf("Enter Something: ");
char name[100];
scanf("%99[help]", name);
printf("%s",name);
}
I face no problem when i enter help me
as the printed word is help
. However when i enter I need help
it prints garbage. Please help how to fix this problem?
My target is to match the word help
entered anywhere along the input, e.g.,
"This is a test, i need help in this"
Should detect help
.
%[\n]
doesn't automatically mean you can write%[help]
. You're leaping to conclusions and making assumptions: not good in programming! (I'm not even convinced that it's valid for\n
.) What are you actually trying to achieve here? – Lightness Races in Orbit