There are a bunch of different ways you can skin this cat; possibly the easiest is to simply determine where to start and where to stop.
All strings are arrays, starring at zero and ending at some point with a
NULL (
\0). So you use the
strlen() function to find out how long your incoming string is, subtract 10 from that value then copy character by character to your destination string until you hit the
NULL (and be sure to copy the NULL!). Something like this will give the starting point and the length (the stopping point):
Code:
n = strlen (src); /* how long is src */
i = n - 10 /* starting point in src */
j = 0; /* starting point in dest */
You need to make sure that
i is not less than zero -- even if you believe that your source string is always going to be some length greater than ten you need to cover yourself when it isn't, just in case. So check it
Then all you need to do is
Code:
for ( ; i < n && src [i] != '\0'; i++) {
dest[j] = src[i];
j++;
}
dest[j] = '\0';
Here's the whole thing
Code:
#include <stdio.h>
#include <string.h>
void main (void)
{
int i, j, n;
char src [1024] = "this is a long string of stuff to see what happens";
char dest [1024];
n = strlen (src); /* how long is src */
i = n - 10; /* starting point in src */
j = 0; /* starting point in dest */
/* check length of src */
if (i < 0)
i = n;
for ( ; i < n && src [i] != '\0'; i++) {
dest [j] = src [i];
j++;
}
dest [j] = '\0';
printf ("%s\n", src);
printf ("%s\n", dest);
}
And, when executed
Code:
this is a long string of stuff to see what happens
at happens
Hope this helps some.