I'm triying to make a function to get something from the middle of another 2 strings, this is what I've got..
The code goes trough the string using it's lenght in a for loop, while it goes trough, it starts finding the letters from the first item and also increments j, to look for the other letter and if it is not found, it resets it, then if j is equal to the first item's lenght, it continues with the second one and it does the same, but if it is equal to the second item lenght it returns the string in between
Code:
char *getFromMiddle(char *string, int l_string, char *first, int l_first, char *second, int l_second) { // the function has to return a char pointer, is it well declared?
int i, j, x, f_pos, s_pos, l_middle;
char t, *middle;
printf("Declara variables \n");
for(i=0;i<l_string;i++) {
printf("For numero: %i \n", i);
if(x != 1) {
if(*string[i] == *first[j]) { //trouble here
printf("Igualdad entre i nš=%i(%c) con j nš=%i(%c): %i \n", i, string[i], j, first[j]);
j++;
if(j == l_first) {
printf("J(%i) = l_first(%i)",j ,i);
f_pos = i;
j = 0;
x = 1;
}
}
else {
printf("No se encontro igualdad en i->%i(%c) == j->%i(%c)",i ,string[i], j, first[j]);
j = 0;
}
}
else {
if(*string[i] == *second[j]) { // also here
j++;
if(j == l_second) {
s_pos = i-l_second;
l_middle = s_pos-f_pos;
middle = malloc(sizeof(t)*(l_middle+1));
for(x=0;x<l_middle;x++) {
*middle[x] == *string[f_pos];
f_pos++;
}
*middle[l_middle+1] = '\0';
return middle; // is it middle well returned?
}
}
else {
j = 0;
}
}
}
return 0; // return 0 if nothing was found
}
I'm having troubles with the pointers and the char arrays, how can I use them as a classic char array? Also I don't really know if the return variable will be a char pointer..
Thanks