Just pass the address of the first letter you want to copy: strcpy(dest, &c[13]).

If you want to end before the string ends, use strncpy or, better, memcpy (don't forget to 0-terminate the copied string).

Answer from Daniel Fischer on Stack Overflow
🌐
LinuxQuestions.org
linuxquestions.org › questions › linux-newbie-8 › copy-last-characters-of-strings-4175422601
copy last characters of strings?
Hello Everyone, Need urgent help. From a string how can i copy last some charterers. like by using this function strncpy (str2,str1,5); will copy
Discussions

c - copy specific characters from a string to another string - Stack Overflow
They think it begins wherever you tell them that it does, so you can freely pass them a pointer into the middle of the string if that is what you need. ... i have another question: if i save in str_cp the word "are" and i use another time the strncpy to copy from str the last 3 chars ex "you" ... More on stackoverflow.com
🌐 stackoverflow.com
url - How to Compare Last n Characters of A String to Another String in C - Stack Overflow
Imagine that I have two strings, one of them is a url like "/sdcard/test.avi" and the other one is"/sdcard/test.mkv". I want to write an if statement that looks whether the last four characters of ... More on stackoverflow.com
🌐 stackoverflow.com
c++ get the last (n) char in a string - Stack Overflow
Finds the last character equal to one of characters in the given character sequence. Search finishes at pos, i.e. only the substring [0, pos] is considered in the search. If npos is passed as pos whole string will be searched. ... That's not an answer, it's a guess. 2010-12-15T22:54:39.21Z+00:00 ... Seems like a pretty good guess to me. Can... More on stackoverflow.com
🌐 stackoverflow.com
C - copy the last characters from an unknown length string - Stack Overflow
If one string does not hate a dt and last free character you can cause an access violation. ... offset = len-4-1; --> offset = len-4; strlen does not count null terminator. 2016-05-03T13:57:19.363Z+00:00 ... Right, I have fixed it. 2016-05-03T14:04:49.083Z+00:00 ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Results of the October 2025 Community Asks Sprint: copy ... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 3
5
char *lastN(const char *str, size_t n)
{
    size_t len = strlen(str);
    return (char *)str + len - n;
}


int main(void)
{
    printf("`%s`\n", lastN("1234567890", 4));
}
2 of 3
2

The header <string.h> contains function strlen that returns the length of a passed string. The function is declared like

    size_t strlen(const char *s);

Strictly speaking the last character of a string is its terminating zero character '\0'. But it seems by the last n characters of a string you mean n characters before the terminating zero.

To be able to get a pointer to the last n characters of a string the string should have at least n characters.

You could write a function the following way. If the passed string contains less than n characters then the function returns a pointer to the string itself.

char * last_n( const char *s, size_t n )
{
    size_t length = strlen( s );

    return ( char * )( length < n ? s : s + length - n );
} 

Here is a demonstrative program.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char * last_n( const char *s, size_t n )
{
    size_t length = strlen( s );

    return ( char * )( length < n ? s : s + length - n );
} 

int main(void) 
{
    char s[] = "Hello world!";
    
    char *p = last_n( s, 6 );
    
    puts( p );
    
    for ( char *current = p; *current != '\0'; ++current )
    {
        *current = toupper( ( unsigned char )*current );
        putchar( *current );
    }
    putchar( '\n' );
    
    return 0;
}

The program output is

world!
WORLD!

If you need to obtain the index where the last n characters of a string start you can write for example

char *p = last_n( s, n );
size_t pos = p - s;

Or just

size_t pos = last_n( s, n ) - s;
🌐
IBM
ibm.com › docs › en › zos › 2.3.0
strncpy() — Copy string
We cannot provide a description for this page right now
🌐
Thinkage
thinkage.ca › gcos › expl › c › lib › strncp.html
STRNCPY - copy characters from string.
This must be able to hold at least N characters. ... "strncpy" copies at most N characters from the string "s2" into the area of memory pointed to by "s1". If it encounters a '\0' before it has copied N characters, it pads "s1" to N characters by adding '\0' characters.
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_strncpy.htm
C library - strncpy() function
Below the example shows the utilization of custom string copy with fixed length. #include <stdio.h> #include <string.h> void customStrncpy(char* dest, const char* src, size_t n) { size_t i; for (i = 0; i < n && src[i] != '\0'; i++) { dest[i] = src[i]; } while (i < n) { // Fill remaining characters with null terminators dest[i] = '\0'; i++; } } int main() { char source[] = "String Copy!"; // Fixed length char destination[15]; customStrncpy(destination, source, 10); printf("Custom copied string: %s\n", destination); return 0; }
🌐
W3Resource
w3resource.com › c-programming › string › c-strncpy.php
C strncpy() function
3 weeks ago - C strncpy() function (string.h): The strncpy() function is used to copy n characters of string2 to string1. If n is less than or equal to the length of string2, a null character (\0) is not appended to the copied string.
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2017 › 11 › c-strncpy-function
C strncpy() Function – C tutorial
September 11, 2022 - * Here string str2 is destination string in which we are copying the * specified string */ strcpy(str2, "welcome to beginnersbook.com"); /* In this case we are doing a limited copy using the strncpy() * function. We are copying only the 7 characters of string str2 to str1 */ strncpy(str1, str2, ...
🌐
Sololearn
sololearn.com › en › Discuss › 1352696 › how-to-print-last-n-characters-of-a-string
How to print last n characters of a string?
June 17, 2018 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 371-how-do-i-get-last-character-string.html
How do I get the last character of a string? - C Board
August 26, 2001 - strlen() returns the length of a string not counting the null byte so :- startaddressofstring+strlen(string)-1 will give you the address of the last character.From there all you need do is dereference that pointer and place a new value in there. Free the weed!! Class B to class C is not good enough!!
🌐
Eskimo
eskimo.com › ~scs › cclass › notes › sx8.html
Chapter 8: Strings
mystrcpy(char dest[], char src[]) { int i = 0; while(src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; } We've called it mystrcpy instead of strcpy so that it won't clash with the version that's already in the standard library. Its operation is simple: it looks at characters in the src string one at a time, and as long as they're not \0, assigns them, one by one, to the corresponding positions in the dest string. When it's done, it terminates the dest string by appending a \0. (After exiting the while loop, i is guaranteed to have a value one greater than the subscript of the last character in src.)
🌐
Reddit
reddit.com › r/c_programming › safest way to copy a string?
r/C_Programming on Reddit: Safest way to copy a string?
May 7, 2023 -

I just fell foul of the fact that strncpy does not add an old terminator if the destination buffer is shorter than the source string. Is there a single function standard library replacement that I could drop in to the various places strncpy is used that would copy a null terminated string up to the length of the destination buffer, guaranteeing early (but correct) termination of the destination string, if the destination buffer is too short?

Edit:

  • Yes, I do need C-null terminated strings. This C API is called by something else that provides a buffer for me to copy into, with the expectation that it’s null terminated

Edit 2:

  • I know I can write a helper function that’s shared across relevant parts of the code, but I don’t want to do that because then each of those modules that need the function becomes coupled to a shared helper header file, which is fine in isolation but “oh I want to use this code in another project, better make sure I take all the misc dependencies” is best avoided. Necessary if necessary, but if possible using a standard function, even better.

🌐
Quora
quora.com › How-do-I-copy-a-string-in-C
How to copy a string in C - Quora
Either use the standard C library routine strcpy() (with #include <string.h>) or use a loop to copy from one character array to another character array. Note that you will have to allocate memory (either automatically or dynamically) for the ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › copy-n-characters-from-one-string-to-another-without-strncat
Copy N Characters from One String to Another Without strncat() - GeeksforGeeks
July 23, 2025 - After both strings have been copied, manually append the null character to the end of result. ... // C program to illustrate how to concatenate the first n // characters from one string to another without using the // strncat() function in C.