Hey everyone, I'm kinda stuck in this: The purpose of the program is to crop a string between two given keywords. Example: if the keywords are "<dt>" and "</dt>" and the string is "<dt>TESTE</dt>" the program should print "TESTE".
The prg is like this
Code:
int main(){
char *string1="<dt>TESTE</dt>";
char *string2;
int inicio,final;
inicio = searchString(string1,"<dt>");
final = searchString(string1,"</dt>");
cropString(string2,string1,inicio+4,final-1);
printf("%s\n%s",string1,string2);
return 0;
}
I've implemented a cropString(string2,string1,n1,n2) function that copies from string1[n1] until string2[n2] (included) and returns the given string in string2, and I guess it's working ok, but i think it doesnt put the '\0' at the end of my string.
Code:
void cropString(char* string2, char* string1,int n1,int n2){
if ( (n1<0) || (n2<0) || (n2<n1) || (n1>strlen(string1)) || (n2>strlen(string1)) )
{
printf("Invalid arguments...");
abort();
}
strncpy(string2,string1+n1,n2-n1+1);
strcat(string2,"\n\n\n");
}
And the searchString(char* string1,char* keyWord)
returns a integer value that represents where the string keyWord starts in string1 (returns -1 if not found or null keyWord)
But when I call printf it shows me this:
The second line should be TESTE
That seems like undefined behavior... Any1 knows what's happening?