You can use pointer arithmetic and the function memcpy:

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

int main( void )
{
    char str1[] = "123copy321";
    char str2[5];

    //copy str1[3] up to and including str1[6] to str2
    memcpy( str2, str1 + 3, 4 );

    //add terminating null character to str2
    str2[4] = '\0';

    printf( "%s\n", str1 );
    printf( "%s\n", str2 );
}

This program has the following output:

123copy321
copy
Answer from Andreas Wenzel on Stack Overflow
๐ŸŒ
Learn Java
javatutoring.com โ€บ c-program-to-copy-one-string-to-another
C Program To Copy One String To Another String | 4 Simple Ways
January 11, 2026 - The main() function calls the recursion function stringcopy() by passing s1,s2,0 as arguments. ... The strcpy(string1, string2) is the one of the string library function to copy one string to another string.
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ examples โ€บ string-copy
C Program to Copy String Without Using strcpy()
However, in this example, we will copy a string manually without using the strcpy() function. #include <stdio.h> int main() { char s1[100], s2[100], i; printf("Enter string s1: "); fgets(s1, sizeof(s1), stdin); for (i = 0; s1[i] != '\0'; ++i) { s2[i] = s1[i]; } s2[i] = '\0'; printf("String ...
Discussions

Are there better ways to copy one string to another?
C doesn't really have a string data type that is something CS50 just made up to make the early weeks easier a string is actually a char * that's called a character pointer it does NOT hold a character it holds a memory address so this string str1 = "this is 26 characters12345"; string str2; actually means char *str1 = "this is 26 characters12345"; char *str2; which actually means your computer has 32 gigs or RAM (little blocks of memory numbered 1 to one billion) str1 does NOT contain the letter 't' str1 contains the number (address) to one of those blocks we have no idea what that number is --- it is automatically assigned by the compiler but let's say that number is 132 so if we went to memory block 132 we would fine the letter 't' and if we went to memory block 133 we would fine the letter 'h' and if we went to memory block 134 we would fine the letter 'i' and if we went to memory block 135 we would fine the letter 's' and so on str2 also contains the address of a block of memory that was automatically assigned to it if we were to go to that memory block what would we find ? who knows ??? what's going to be in there is what was in there after the computer booted up could be any random thing the point is that memory block does NOT belong to you neither does the memory right after it, neither does the next memory block, or the next, or the next ... so when you say str2[i] = str1[i]; what you're really saying is i = 0 so take what is at str1's memory block number + 0 and put it into the memory block at str2's memory block + 0 i = 1 so take what is at str1's memory block number + 1 and put it into the memory block at str2's memory block + 1 and the computer says "No, you don't own the memory block at str2" and then your program crashes but when you do char str2[26] and then do i = 0 so take what is at str1's memory block number + 0 and put it into the memory block at str2's memory block + 0 i = 1 so take what is at str1's memory block number + 1 and put it into the memory block at str2's memory block + 1 the computer says, no problem because you made space for str2, you own str2 and the 26 memory blocks after it More on reddit.com
๐ŸŒ r/cs50
5
3
September 16, 2022
copy character from string to another string in C - Stack Overflow
I have a string AAbbCC what I need is to copy the first two and add them to an array then copy the middle two and add them to an array and finally the last two and add them to an array. this is wh... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Copy string in C
A C string is a nul-terminated character array. The C language does not allow assigning the contents of an array to another array. As noted by Barry, you must copy the individual characters one by one from the source array to the destination array. More on learn.microsoft.com
๐ŸŒ learn.microsoft.com
3
0
September 12, 2020
How to copy from a string to another string in Java? - Stack Overflow
I have two strings str1 and str2. I am trying to copy some letters from one string to the other by using charAt. I know that I can use string copy but I want some characters not all. How can copy a subString from a string to another string in Java? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 2
2

You can use pointer arithmetic and the function memcpy:

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

int main( void )
{
    char str1[] = "123copy321";
    char str2[5];

    //copy str1[3] up to and including str1[6] to str2
    memcpy( str2, str1 + 3, 4 );

    //add terminating null character to str2
    str2[4] = '\0';

    printf( "%s\n", str1 );
    printf( "%s\n", str2 );
}

This program has the following output:

123copy321
copy
2 of 2
0

With theFunctionINeed(str1, str2, 3, 6); there are a number of issues:

  1. Source string may be less than 3.

  2. Available sub-string length may be less than 4.

  3. Destination array may be too small.

  4. Unusual to pass in the first and last index to copy. This prevents forming a zero-length sub-string. More idiomatic to pass in beginning and 1) length or 2) index of one-past.

  5. How about returning something useful, like was the destination big enough?

Alternative untested sample code follows. restrict means the two pointers should not point to overlapping memory.

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

// Return `destination` when large enough
// otherwise return NULL when `size` was too small. 
bool SubString(size_t destination_size, char *restrict destination,
    const char *restrict source, size_t offset, size_t length) {
  if (destination_size == 0) {
    return NULL;
  }
  destination[0] = '\0';

  // Quickly search for the null character among the first `offset` characters of the source.
  if (memchr(source, '\0', offset)) {
    return destination;
  }

  destination_size--;
  size_t destination_length = length <= destination_size ? length : destination_size;
  strncat(destination, source + offset, destination_length);
  return length <= destination_size ? destination : NULL;
}
๐ŸŒ
YouTube
youtube.com โ€บ watch
c program to copy one string into another | strcpy() function in c - YouTube
What is String? With Example Program...!๐Ÿ‘‡https://www.youtube.com/playlist?list=PLqleLpAMfxGAIBEDg0mvnCbS8nEvwWbUqPlease Subscribe our Channel...!Learn Codin...
Published ย  November 5, 2019
๐ŸŒ
CodeScracker
codescracker.com โ€บ c โ€บ program โ€บ c-program-copy-string.htm
C Program to Copy One String to Another
Now copy the string into another variable, say str2, using the strcpy() function. The strcpy() function takes two arguments. The first argument is the target variable where the string is going to be copied. The second argument is the source variable. The value of this variable is initialized ...
๐ŸŒ
Java Guides
javaguides.net โ€บ 2023 โ€บ 09 โ€บ c-program-to-copy-one-string-to-another-using-pointers.html
C Program to Copy One String to Another Using Pointers
September 12, 2023 - 1. We start with the declaration of two character arrays, source and destination, to store our source and copied strings. 2. The copyStrings function is tasked with copying one string to another. This function uses two pointers (source and ...
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/cs50 โ€บ are there better ways to copy one string to another?
r/cs50 on Reddit: Are there better ways to copy one string to another?
September 16, 2022 -

Like an idiot, I was initially trying to copy all the elements of a string to another string the way one copies an array of int -

// both the strings str1 & str2 are of same length
for (int i = 0, n = strlen(str1); i < n; i++)
{
    str2[i] = str1[i];
}

And this was giving me a "Segmentation fault (core dumped)" every time I executed the program (compiling was okay). After scratching my head for sometime I decided to make str2 an array of chars.

char str2[26];
for (int i = 0, n = strlen(str1); i < n; i++)
{
    str2[i] = str1[i];
}

Voila! It is working perfectly. I just want to know why this segmentation fault happens in the first case. And if there are any better ways to copy strings. Now before someone suggests using "strcpy" from <string.h> , the same fault arises if you copy two strings & nothing goes wrong if str2 is made an array. So, yeah any explanations as to why this happens ? Thanks for reading my post.

Top answer
1 of 2
2
C doesn't really have a string data type that is something CS50 just made up to make the early weeks easier a string is actually a char * that's called a character pointer it does NOT hold a character it holds a memory address so this string str1 = "this is 26 characters12345"; string str2; actually means char *str1 = "this is 26 characters12345"; char *str2; which actually means your computer has 32 gigs or RAM (little blocks of memory numbered 1 to one billion) str1 does NOT contain the letter 't' str1 contains the number (address) to one of those blocks we have no idea what that number is --- it is automatically assigned by the compiler but let's say that number is 132 so if we went to memory block 132 we would fine the letter 't' and if we went to memory block 133 we would fine the letter 'h' and if we went to memory block 134 we would fine the letter 'i' and if we went to memory block 135 we would fine the letter 's' and so on str2 also contains the address of a block of memory that was automatically assigned to it if we were to go to that memory block what would we find ? who knows ??? what's going to be in there is what was in there after the computer booted up could be any random thing the point is that memory block does NOT belong to you neither does the memory right after it, neither does the next memory block, or the next, or the next ... so when you say str2[i] = str1[i]; what you're really saying is i = 0 so take what is at str1's memory block number + 0 and put it into the memory block at str2's memory block + 0 i = 1 so take what is at str1's memory block number + 1 and put it into the memory block at str2's memory block + 1 and the computer says "No, you don't own the memory block at str2" and then your program crashes but when you do char str2[26] and then do i = 0 so take what is at str1's memory block number + 0 and put it into the memory block at str2's memory block + 0 i = 1 so take what is at str1's memory block number + 1 and put it into the memory block at str2's memory block + 1 the computer says, no problem because you made space for str2, you own str2 and the 26 memory blocks after it
2 of 2
2
in the first one you say both the strings str1 & str2 are of same length what are their types ? how did you declare them ? how did you initialize them ? and finally, have you learned about the null terminator yet ? i'm guessing no because I don't see it in either example
๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ c program to copy one string to another string
C program to copy one string to another string - Codeforwin
July 20, 2025 - This function is present in string.h header file. /** * C program to copy one string to another string using strcpy() */ #include <stdio.h> #include <string.h> #define MAX_SIZE 100 // Maximum size of string int main() { char text1[MAX_SIZE], text2[MAX_SIZE]; /* Input original string from user */ printf("Enter any string: "); gets(text1); /* Copy text1 to text2 using strcpy() */ strcpy(text2, text1); printf("First string = %s\n", text1); printf("Second string = %s\n", text2); return 0; }
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-copy-a-string-using-strcpy-function-in-c
How to copy a string using strcpy() function in C
The strcpy() function is a built-in library function, declared in the string.h header file. It copies the source string to the destination string until a null character (\0) is reached.
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ library-function โ€บ string.h โ€บ strcpy
C strcpy() - C Standard Library
#include <stdio.h> #include <string.h> int main() { char str1[20] = "C programming"; char str2[20]; // copying str1 to str2 strcpy(str2, str1); puts(str2); // C programming return 0; }
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ string โ€บ c-string-exercise-8.php
C Program: Copy one string into another string - w3resource
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char str1[100], str2[100]; // Declare two character arrays to store strings int i; // Declare a variable for iteration printf("\n\nCopy one string into another string :\n"); // Display information about the task printf("-----------------------------------------\n"); printf("Input the string : "); fgets(str1, sizeof str1, stdin); // Read a string from the standard input (keyboard) /* Copies string1 to string2 character by character */ i = 0; // Initialize the iteration variable while (str1[i] != '\0') { // Loop until the en
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ strcpy-in-c
strcpy() in C - GeeksforGeeks
March 6, 2026 - In C, strcpy() is a built-in function used to copy one string into another.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c-programs โ€บ c-program-to-copy-string-string-copy-strcpy.aspx
C program to copy one string into another | Implementing strcpy() in C
In this program, we will read a string and copy the string into another using stringCopy() function which is implemented by own. #include <stdio.h> /******************************************************** * function name :stringCpy * Parameter :s1,s2 : string * Description : copies string ...
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ recursion โ€บ c-recursion-exercise-19.php
C Program: Copy One string to another - w3resource
November 1, 2025 - C programming, exercises, solution: Write a program in C to copy one string to another using recursion.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-copy-a-string-in-C
How to copy a string in C - Quora
Answer (1 of 2): Three major ways. 1. strncpy [code]char* src = "a const string to be copied"; char dest[28] = {0}; char *strncpy(char *dest, const char *src, size_t n); dest[n]= '\0'; // terminate manually [/code] 1. strncpy copies a char array src into another char array dest up to a given ...
๐ŸŒ
Studytonight
studytonight.com โ€บ c-programs โ€บ c-program-to-copy-one-string-to-another-using-recursion
C Program to Copy One String to Another using Recursion - Studytonight
January 10, 2022 - #include <stdio.h> void recur(char [], char [], int); int main() { char str[30], str1[30]; printf("Enter The String: "); scanf("%[^\n]s", str); recur(str, str1, 0); printf("Executed Successfully\n"); printf("The input String: %s\n", str); printf("The Copied String: %s\n", str1); return 0; } void recur(char str[], char str1[], int index) { str1[index] = str[index]; if (str[index] == '\0') return; recur(str, str1, index + 1); } ... Try our new interactive courses. ... Over 20,000+ students enrolled. ... I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development.