Do it like
strncpy (dest, ar + 7, 2);
basically
strncpy (destination, source + start_index, number_of_chars);
The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-termiβ nated.
Therefore, you need to zero out the end of the line manually:
dest[nos_of_chars] = '\0';
UPDATE
You can use something like this:
char *make_substring (char *src, int start, int end) { int nos_of_chars = end - start + 1; char *dest; if (nos_of_chars < 0) { return NULL; } dest = malloc (sizeof (char) * (nos_of_chars + 1)); dest[nos_of_chars] = '\0'; strncpy (dest, src + start, nos_of_chars); return dest; }
When you use C ++, do not use char strings for processing instead, using the string class.
source share