As a homework, I have to make a program which replaces all the words made of 3 letters with a "*".
For example:
input: cod is everything and I love it
output: * is everything * I love it
Here is my program:
int main()
{
char cuvant[101], final[101];
char* pch;
cin.get(cuvant, 100);
pch = strtok(cuvant, " ");
while (pch != NULL)
{
if (strlen(pch) == 3)
{
strcat(final, " *");
}
else
{
strcat(final, " ");
strcat(final, pch);
}
pch = strtok(NULL, " ");
}
cout << final;
}
I don't get any error - but the output is blank. Also, the program doesn't exit with the code 0 (as it should be), it says "exited with code -1073741819". So obviously something is going on.
Am I using strtok/strcat wrong?
I feel like strcat can't be used in my case and this is the reason for the error, but I'm not sure. Am I right?
If so, how can I solve my problem?
Thanks.