C Program To Copy One String To Another String

C Program To Copy One String To Another String

C program to copy one string to another string; Through this tutorial, we will learn how to copy one string to another string using for loop, function, recursion, and strcpy() built-in library function in c programs.

Programs To Copy One String To Another String in C

Using the following program to copy one string to another string using for loop, function, recursion, and strcpy() built-in library function in c:

  • C Program To Copy One String To Another String using For Loop
  • C Program To Copy One String To Another String using Function
  • C Program To Copy One String To Another String using recursion
  • C Program To Copy One String To Another String using Strcyp()

C Program To Copy One String To Another String using For Loop

#include <stdio.h>
 
int main()
{
    char s1[1000],s2[1000];  
    int i;
 
    printf("Enter any string: ");
    gets(s1);
    for(i=0;s1[i]!='
#include <stdio.h>
int main()
{
char s1[1000],s2[1000];  
int i;
printf("Enter any string: ");
gets(s1);
for(i=0;s1[i]!='\0';i++)  //  or  for(i=0;s1[i];i++)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("original string s1='%s'\n",s1);
printf("copied string   s2='%s'",s2);
return 0;
}
';i++) // or for(i=0;s1[i];i++) { s2[i]=s1[i]; } s2[i]='
#include <stdio.h>
int main()
{
char s1[1000],s2[1000];  
int i;
printf("Enter any string: ");
gets(s1);
for(i=0;s1[i]!='\0';i++)  //  or  for(i=0;s1[i];i++)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("original string s1='%s'\n",s1);
printf("copied string   s2='%s'",s2);
return 0;
}
'; printf("original string s1='%s'\n",s1); printf("copied string s2='%s'",s2); return 0; }

The output of the above c program; as follows:

Enter any string: tutsmake
original string s1='tutsmake'
copied string   s2='tutsmake'

C Program To Copy One String To Another String using Function

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <string.h>
 
void stringcopy(char *s1,char *s2)
{
	int i;
    for(i=0;s2[i]=s1[i];i++);
    
    s2[i]='
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <string.h>
void stringcopy(char *s1,char *s2)
{
int i;
for(i=0;s2[i]=s1[i];i++);
s2[i]='\0';
}
int main()
{
char s1[1000],s2[1000];  
int i;
printf("Enter any string: ");
gets(s1);
stringcopy(s1,s2);
printf("original string s1='%s'\n",s1);
printf("copied string   s2='%s'",s2);
return 0;
}
'; } int main() { char s1[1000],s2[1000]; int i; printf("Enter any string: "); gets(s1); stringcopy(s1,s2); printf("original string s1='%s'\n",s1); printf("copied string s2='%s'",s2); return 0; }

The output of the above c program; as follows:

Enter any string: hellllo
original string s1='hellllo'
copied string   s2='hellllo'

C Program To Copy One String To Another String using recursion

void stringcopy(char *s1,char *s2,int i)
{
    if(s1[i]=='
void stringcopy(char *s1,char *s2,int i)
{
if(s1[i]=='\0')
{
s2[i]='\0';
return;
}  
else
{
s2[i]=s1[i];
stringcopy(s1,s2,++i);
}
}
int main()
{
char s1[1000],s2[1000];  
int i;
printf("Enter any string: ");
gets(s1);
stringcopy(s1,s2,0);
printf("original string s1='%s'\n",s1);
printf("copied string   s2='%s'",s2);
return 0;
}
') { s2[i]='
void stringcopy(char *s1,char *s2,int i)
{
if(s1[i]=='\0')
{
s2[i]='\0';
return;
}  
else
{
s2[i]=s1[i];
stringcopy(s1,s2,++i);
}
}
int main()
{
char s1[1000],s2[1000];  
int i;
printf("Enter any string: ");
gets(s1);
stringcopy(s1,s2,0);
printf("original string s1='%s'\n",s1);
printf("copied string   s2='%s'",s2);
return 0;
}
'; return; } else { s2[i]=s1[i]; stringcopy(s1,s2,++i); } } int main() { char s1[1000],s2[1000]; int i; printf("Enter any string: "); gets(s1); stringcopy(s1,s2,0); printf("original string s1='%s'\n",s1); printf("copied string s2='%s'",s2); return 0; }

The output of the above c program; as follows:

Enter any string: hyyyy
original string s1='hyyyy'
copied string   s2='hyyyy'

C Program To Copy One String To Another String using Strcyp()

#include <stdio.h>
#include <string.h>
 
 
int main()
{
   char s1[1000],s2[1000];  
    int i;
 
    printf("Enter any string: ");
    gets(s1);
    strcpy(s2,s1);
    printf("original string s1='%s'\n",s1);
    printf("copied string   s2='%s'",s2);
    return 0;
    
}

The output of the above c program; as follows:

Enter any string: welcome
original string s1='welcome'
copied string   s2='welcome'

Recommended C Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *