I'm inputting the string str
and want to print all of its words that contain the inputted character c
on the position n
(so if n = 1, it's the first character). I'm trying to do this using strtok()
but I'm getting a weird crash. Any tips?
int main()
{
char str[100]; gets(str);
while(getchar()!='\n'); ///so that n or c don't scan a newline in them
int n; scanf("%d",&n);
char c; scanf("%c",&c);
char* token = strtok(str, " ");
while (token != NULL) {
if(token[n-1]==c){
printf("%s\n", token);
}
token = strtok(NULL, " ");
}
return 0;
}
I inputted the following:
Hi i like mint
2
i
Then the program suddenly crashes with the message:
Problem.exe has stopped working...