hi. i have a work to do that consists on a given char pointer to a chain and a also given char pointer to a string. i have to search that chain in the string, and return the position where the beginning of the two it's equal. but only if they are equal.
example:
string is "abcd" and the chain is "bc". want to find the chain, and if it exists, the position return will be 2.
in the work it says that we should use sprintf() too.
here is my code, which is incomplet yet.
Code:
char* pos(char* C,char* S){
char *str = S;
char *chain = C;
int i, j, temp;
int lengthStr = strlength(C);
int lengthChain = strlength(S);
char *posit= malloc(4*sizeof(lengthStr));
for(i=0; i < lengthStr; i++){
if( *(str+i) == *chain){
sprintf(posit++, "%d", i);
for(j=0; j < lengthChain && *str==*chain; j++){
if( *(str) != *(chain))
posit--;
else{
str++;
chain++;
}
}
}
//else
//*posit = 0;
}
free(posit);
return posit;
}
my doubt is how to use sprintf() and where. if i have to use malloc() and free(), and maybe a little help for the code.
thanks, and sorry if the text is confused
other examples:
pos(“AGA”,”ACTGCA”)
return:
0
pos(“AGA”, “ATAGATA”)
return:
3
pos(“AGA”, “AGATAGA”)
return
1 5