I need help with breaking a string into an array. I got it to work without storing the info and just printing the tokens. But for this prog, I need to store the tokens strtok made and use a binary search to do a strncmp with 2 elements each being from a different array.
./file "Example input: 'Cause I'm Batman"
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char delims[] = " ";
char *result = NULL;
int i = 1;
int j = 0;
char sent[1000];
result = strtok(argv[1], delims);
sent[0] = *result;
while(result != NULL)
{
result = strtok(NULL, delims);
sent[i] = *result;
i++;
}
while(j < i)
{
printf(" %p\n", &sent[j]);
j++; //Forgot to add it in first time around
}
return 0;
}
Problem is I'm getting a segmentation fault and I can't seem to get it to store the tokens into an array and I don't understand why. Is it a pointer issue? Passing incompatible data types? Something else?
Edit: Wanted output: "Example" "input:" "'Cause" "I'm" "Batman"
Any help would be great.
sent[i] = *result;attempts to read NULLsent[i] = *result;stores the first character of each token in thesentarray. It doesn't seem like what you want. Also,while (j < i)is an endless loop.string str ="something";usechar * ch = st.c_str();