1
votes

In the following lines of code, I am attempting to substitute the key string, in for the variable strings when it is found in a string. for instance if i had a string "hello VARIABLE world", the KEY value would be put in place of the VARIABLE value making the string "hello KEY world" instead of the original string. my theory for that was tokenize the string up to the first character of the variable (in the string) (newfirst), tokenize the variable characters (newtrash), and then tokenize the remainder of the string (newsecond). Then concatenize newfirst + key + newsecond. The issue is, myVariables[i].variable[0] and myVariables[i].variable[varlength - 1], are reading as chars into the strtok functions in the first two lines and the compiler is asking for a const char*. any idea how i could format the first and last values of these strings work in the strtok function?

SIMPLER WAY TO PHRASE THIS: how do i pass an individual char from a char array (user input so can NOT be hardcoded) as the deliminator character in the strtok function?

** to be clear this is not the whole program, just the lines of concern to the problem

  strcpy(newfirst, strtok(testString, myVariables[i].variable[0]));
   strcpy(newtrash, strtok( NULL, myVariables[i].variable[varlength - 1]));
   strcpy(newsecond, strtok(NULL, "\n"));
   strcat(newfirst, myVariables[i].key);
   strcat(newfirst, newsecond);


  struct variablePairs {
     char variable[20];                 
     char key[20];                  
 };
2
Not an answer. May save your debugging time stackoverflow.com/questions/4031075/…CCoder

2 Answers

3
votes

You don't. Strtok is probably not the right solution for what you are trying to do, and you seem to be misunderstanding what it does. The delim argument to strtok is actually a SET of characters, expressed as a string. strtok scans through the input string until it finds any single character that is in the delimeter string and considers that the start of the delimeter. It then looks for additional characters that are anywhere in the delimeter string and considers them as delimeters as well.

Now its not at all clear what you are trying to do, but it looks like you are searching for a substring VARIABLE and replacing that with some other string. strstr is probably what you want here. Something like:

if ((p = strstr(input, myVariables[i].variable))) {
    strncpy(output, input, p-input);
    strcat(output, myVariables[i].key);
    strcat(output, p+strlen(myVariables[i].variable));
} else {
    /* variable not found */
    strcpy(output, input);
}

This will find the variable string even if its embedded in another word -- if you want to avoid that, you need to also check the make sure the match is surrounded by spaces or at the end of the string:

len = strlen(myVariables[i].variable);
if ((p = strstr(input, myVariables[i].variable)) &&
    (p == input || isspace(p[-1])) &&
    (isspace(p[len]) || p[len] == 0))

depending on how you want to deal with numbers and punctuation, you might want to replace the isspace calls with !isalpha or !isalnum

0
votes

this is not the best way to solve your problem:

1.declare a pointer and pass your first or last char into it.

2.remember to end it with '\0' to make it parsable to a const char.

3.use the (const char *)p form in your strtok.

char x[256]={0};
char *p;

p = malloc(2);
strcpy(x,"abcd");
*p = x[0];
memset(p+1,'\0',1);

printf("\n point p is %s\n",(const char *)p);

output:

point p is a