The thing that immediately leaps out at one is the fact that you're using memory that isn't yours!
That is to say, your variable 'str' is a char - it holds just 1 byte of memory, yet in the scanf statement you're reading an unknown number of chars into just one byte of memory you own and (unknown number of chars - 1) into memory that follows.
You should:
- Allocate some memory, either statically or dynamically
- use scanf_s[^], which allows you to specify the max # of chars copied
Either use:
const int bufSize = 1024;
char str[bufSize];
or
const int bufSize = 1024;
char *str;
str = (char*)calloc(bufSize, 1);
And then use:
scanf_s("%s", str, bufSize-1);
I haven't looked at the rest of the program. This may or may not fix your problem. In any case, it's still an error that you should fix.