0

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...

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
  • 2
    Don't use `gets()`, use `fgets()` instead . Read [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Achal May 25 '19 at 09:56
  • 2
    You're worried about scanning with `"%c"` will read a newline, but then you use `scanf("%d",&n);` which will leave a newline (or space) in the input-buffer for the next `scanf` to read. And by the way, the `"%d"` format reads and ignores (throws away) leading space, so you don't need to "flush" the input for that. *And* there's a very simple solution to get `scanf` to read and ignore leading space for the `"%c"` format as well: `" %c"` (note leading space in string). – Some programmer dude May 25 '19 at 10:08
  • Have you tried to catch the "crash" in a debugger, or otherwise tried to step through the code line by line to see what really happens (which would have made the `"%c"` scanning problem very obvious)? – Some programmer dude May 25 '19 at 10:10
  • OT: Variable (and parameter) names should indicate `content` or `usage` (or better, both) single character names like `n` and `c` are meaningless. – user3629249 May 26 '19 at 16:31
  • OT: the function: `gets()` has been depreciated for years, due to its' problems, and completely removed from the language in the most recent versions of the C language. Your compiler should have told you about this. Suggest using `fgets()` – user3629249 May 26 '19 at 16:34
  • OT: for ease of readability and understanding: 1) please follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* – user3629249 May 26 '19 at 16:37

1 Answers1

0

That while loop didn't seem necessary. Also, instead of gets(), I used fgets(). I moved most of the declarations at the beginning of function. This code now works probably.

#include <stdio.h>
#include <string.h>
int main()
{
    int n; 
    char c, str[100]; 

    fgets(str, 100, stdin);
    scanf("%d %c",&n, &c);

    char* token = strtok(str, " ");

    while (token != NULL) {
        if(token[n-1] == c) {
            printf("%s\n", token);
        }
        token = strtok(NULL, " ");
    }

    return 0;
}

Here is the link where I tested it: https://ideone.com/KQkRrG

kiner_shah
  • 3,939
  • 7
  • 23
  • 37