基本的に私のコードでは、変数とキーのペアがあります。コード、関数 (以下の行の一部を含む) で変数が検出されると、変数はそのキーに置き換えられます。たとえば、ユーザーが文字列 "Hello ABC World" を入力し、変数が ABC = "Great Big" として宣言されている場合、入力文字列は "Hello Great Big World" に変更されます。これは、変数の前と変数の後の文字列の部分を strtok し、newfirst (変数の前の部分) + 変数のキー + newsecond (変数の後の部分) を連結することで実現します。変数が文字列 "Hello World ABC" の末尾にある場合を除き、ほとんどの場合、これはうまく機能します。したがって、私が信じているのは単一の文字「\ n」であり、残っているものをトークン化できないためエラーをスローします。
// Place the part of the string before the variable prescence in newfirst
strcpy(newfirst, strtok(nonVariable[i], myVariables[a].variable));
// Place the part of the string after the variable prescence in newsecond
for(c = 0; c < strlen(newsecond); c++)
{
if(newsecond[c] == ' ')
hasSpaces = 1;
}
if(hasSpaces = 0)
{
strcpy(newsecond, strtok(NULL, "\n"));
}
else
{
strcpy(newsecond, strtok(NULL, " "));
strcpy(newsecond, strtok(NULL, "\n"));
}
// substitute all key values in for their corresponding variables
strcat(newfirst, " ");
strcat(newfirst, myVariables[a].key);
strcat(newfirst, " ");
strcat(newfirst, newsecond);
strcpy(nonVariable[i], newfirst);