C strings need to be null terminated. Ie they need a '\0' character at the end, so common string expressions can tell where the string ends. String functions like strcpy and printf("%s... look for this null termination in their operation. When you do this:

for(i=0; s1[i] != '\0'; i++)
        s2[i] = s1[i];

You fail to copy the null terminator. This means when you print the string, printf will go through the string until it finds the first '\0' character somewhere after the end of your string. This is why you see the extra characters, and in the case where the NULL is outside your strings allotted memory you will actually cause undefined behaviour by accessing unallocated space, which is very very bad.

You either need to null terminate your string by adding:

s2[i] = '\0'

After your for loop. Or even better, use a standard c function strcpy.

Answer from Fantastic Mr Fox on Stack Overflow
🌐
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; }
🌐
CodeScracker
codescracker.com › c › program › c-program-copy-string.htm
C Program to Copy One String to Another
As you can see from the above snapshot, only "codes" are initialized in the variable str1, and the value of str1 gets copied to the str2 variable. Therefore, the above program is correct only when the user doesn't supply any strings with spaces. This program performs the same function as the previous one, but without the use of the library function strcpy():
🌐
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
#include <stdio.h> /******************************************************** * function name :stringCpy * Parameter :s1,s2 : string * Description : copies string s2 into s1 ********************************************************/ void stringCpy(char* s1,char* s2); int main() { char str1[100],str2[100]; printf("Enter string 1: "); scanf("%[^\n]s",str1);//read string with spaces stringCpy(str2,str1); printf("String 1: %s \nString 2: %s\n",str1,str2); return 0; } /******** function definition *******/ void stringCpy(char* s1,char* s2) { int i=0; while(s2[i]!='\0') { s1[i]=s2[i]; i++; } s1[i]='\0'; /*string terminates by NULL*/ }
🌐
Vultr
docs.vultr.com › clang › examples › copy-string-without-using-strcpy
C Program to Copy String Without Using strcpy() | Vultr Docs
December 4, 2024 - Explore various examples that demonstrate different methods to achieve this, enhancing your grasp of pointers, arrays, and character manipulation in C. Initialize the source string and an empty destination string of sufficient size.
🌐
Onlinegdb
learn.onlinegdb.com › c_program_copy_string_without_using_strcpy
C Program to copy string without using strcpy() | Learn Programming step by step
Problem Statement: Write a C Program to copy string without using strcpy() · Required Knowledge: C Input/Output, C Variables, C Datatypes, C for..loop
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-program-copy-string-without-using-strcpy-function
C program to copy string without using strcpy() function - GeeksforGeeks
December 15, 2024 - ... #include <stdio.h> void copy(char ... src to dest copy(src, dest); printf("%s", dest); return 0; } ... Hello, Geeks! The memcpy() function is part of the C standard library and can be used to copy a specified number of bytes ...
🌐
TutorialsPoint
tutorialspoint.com › c-program-to-copy-string-without-using-strcpy-function
C program to copy string without using strcpy() function
#include<stdio.h> main() { char str[50]; //create an empty string to store another string char *myString = "Program to copy a String"; sprintf(str, "%s", myString);//Use sprintf to copy string from myString to str printf("The String is: %s", str); }
Find elsewhere
🌐
Java Guides
javaguides.net › 2023 › 09 › c-program-to-copy-strings-without-using-library-function.html
C Program to Copy Strings without using Library Function
September 12, 2023 - #include <stdio.h> // Include the Standard I/O library int main() { // Start of the main function char source[100], destination[100]; // Declare arrays to store the original and copied strings int i = 0; // Counter variable // Ask the user for the source string printf("Enter the source string: "); scanf("%s", source); // Loop to copy the string while (source[i] != '\0') { destination[i] = source[i]; i++; } destination[i] = '\0'; // Null terminate the destination string printf("Copied string is: %s\n", destination); // Display the copied string return 0; // End the program }
🌐
Log2Base2
log2base2.com › c-examples › string › string-copy.html
C program to copy string without using strcpy() function
#include<stdio.h> int main() { char str[100],copy_str[100]; int i; scanf("%s",str); /* * Let's assume string as "abc" */ for(i = 0; str[i] != '\0'; i++) copy_str[i] = str[i]; /* * i = 0; copy_str[0] = str[0] i.e. 'a' * i = 1; copy_str[1] = str[1] i.e. 'b' * i = 2; copy_str[2] = str[2] i.e. 'c' * i = 3; str[i] == '\0'. It will come out of loop */ copy_str[i] = '\0'; // i = 3; copy_str[3] = '\0'; printf("Copied string = %s",copy_str); return 0; } Run it
🌐
Tutorial Gateway
tutorialgateway.org › c-program-to-copy-string
C program to Copy String without using strcpy
April 4, 2025 - #include <stdio.h> #include <string.h> void CopyString(char str1[], char str2[], int index); int main() { char Str[100], CopyStr[100]; printf("\n Please Enter any text : "); gets(Str); CopyString(Str, CopyStr, 0); printf("\n Original = %s", ...
🌐
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
🌐
YouTube
youtube.com › techaeron
How to copy string without using strcpy() function C programming|| C language tutorial for beginners - YouTube
In this C programming tutorial, we're going to learn how to copy one string into another string without using strcpy() function. strcpy() function is a built...
Published   September 3, 2022
Views   759
🌐
YouTube
youtube.com › watch
50 - STRING COPY WITHOUT USING STRCPY FUNCTION - C PROGRAMMING - YouTube
This program is for copying the content of one string to another string without using string handling function (strcpy( ))
Published   October 30, 2017
Top answer
1 of 2
2

Your program has two bugs:

First

Your function CopyAString does not write a '\0' character to the end of the string. This means that the string s2[] does not have a '\0' character and you would not be able to pass s2[] to functions like printf() or other functions that expect an "input" string to end with '\0'.

However, in your program, this is not a problem because the for loop expects a fixed-length string.

Second

In your program, the following problem is more important:

You pass &str as first argument to CopyAString instead of str.

This means that s1 (the first argument of CopyAString) does not point to the character 'H' of "Hello world" but it points to the first byte of the value stored in the variable str...

Note that the variable str is a pointer: It does not store a "value" (here: the string "Hello world") but it stores an address of a value!

If the string "Hello world" is stored in the RAM at address 0x20 44 00 40 (this means: 0x40004420 on an x86 or ARM computer or 0x20440040 on a PowerPC), the variable str will contain the value 0x20 44 00 40.

s1[0] will be 0x20 (which is the space character). s1[1] will be 0x44 (which is 'D')...

2 of 2
0

For starters you should declare the function like

char * CopyAString( char *s1, const char *s2 );

similarly to the standard string function strcpy.

In this function call

CopyAString( &str1, s2 );

the expression &str1 has the type const char ** and yields the address of the pointer str1 but the function expects an argument expression of the type const char * that points to the first element of a string (the string literal).

Within the function you are not copying the terminating zero character

while (*p1 != '\0') {
    s2[index] = *p1;
    index++;
    p1++;
}

So the destination character array in general will not contain a string.

The function can be defined the following way

char * CopyAString( char *s1, const char *s2 )
{
    for ( char *p = s1; ( *p++ = *s2++ ); );

    return s1;
}

Within main instead of this for loop with the magic number 12 in the condition that invokes undefined behavior

for (int i = 0; i <= 12; i++){
    printf("%c", s2[i]);
}

it is better to write

for ( char *p = s2; *p != '\0'; ++p ){
    printf("%c", *p );
} 
putchar( '\n' );
🌐
Basic C Programs
c4learn.com › c-programs › c-program-to-copy-one-string-into-other.html
C Program to Copy One String into Other Without Using Library Function. - String Programs - c4learn.com
August 4, 2014 - C Program to Concat Two Strings without Using Library Function · C program to Delete all occurrences of Character from the String. C Program to Encode a String and Display Encoded String · C Program to Reverse Letter in Each Word of the Entered String · C Program to Find Length of the String using Pointer · C Program to Count number of words,digits,vowels using pointers ... Program : C Program to Copy One String into Other Without Using Library Function.
🌐
PREP INSTA
prepinsta.com › home › all about c language › program for string copy in c
Program for String Copy in C
January 31, 2023 - Without using strcpy() function. for (i = 0; s1[i] != '\0'; ++i) { s2[i] = s1[i]; } In this we are using a for loop to copy each character of the string into another empty string.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-program-copy-string-without-using-strcpy-function
C program to copy string without using strcpy() function - GeeksforGeeks
December 15, 2024 - ... #include <stdio.h> void copy(char ... src to dest copy(src, dest); printf("%s", dest); return 0; } ... Hello, Geeks! The memcpy() function is part of the C standard library and can be used to copy a specified number of bytes ...