Hi Lets say i have a string "ABCDEFGHIJKLMN"
and my requirment is that i need to extract a substring from position 5-10 of the string. how do i do it in C? (i.e FGHIJK)
Thanks
Hi Lets say i have a string "ABCDEFGHIJKLMN"
and my requirment is that i need to extract a substring from position 5-10 of the string. how do i do it in C? (i.e FGHIJK)
Thanks
If you're allowed to smash the original string, then just put a null-terminator at 10 and return from 5.
i.e.,
Of course if you don't want to modify the original string, then just use memcpy() in the same sort of fashion. Allocating memory for the substring before you copy it in, (from - to) characters, starting from &str[from].Code:char * substr(char * str, size_t from, size_t to) { size_t len = strlen(str); if(from > len || to > len) return NULL; // end the string at to str[to] = '\0'; return &str[from]; }
Last edited by zacs7; 06-22-2009 at 01:10 AM. Reason: WTF, strncpy()? You mean memcpy(). Noob.
You mean like memcpy?You could of course actually TRY.Code:memcpy( tohere, fromhere, len ); tohere[ len ] = '\0';
Quzah.
Hope is the first step on the road to disappointment.