Here is a program in C to copy one string to another string using user defined function. Here two character arrays are defined then prompted to enter the strings and copied to the first string using while() function.
Program in C to Copy a String to Another
Steps:
- At first declare two strings str1 and str2 and integers m, i, flag and j under main function.
- Prompt message to enter the strings and allow to enter strings using gets() function.
- Prompt the message and allow to enter the index where you want to insert in the first string.
- Copy second string to the first string using while() function.
- Print the first string to the screen.
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
char str1[20], str2[20];
int m,i,flag=0,j;
clrscr();
printf("Enter the 1st String");
gets(str1);
printf("Enter the 2nd String");
gets(str2);
printf("Enter the index after which you want to insert 2nd string in 1st :");
scanf("%d", &m);
i=0;
while (i<=m)
{
i++;
}
j=0;
while (str2[j]!='\0')
{
str1[i]=str2[j];
i++;
j++;
if (str1[i]=='\0')
flag=1;
}
if (flag==1)
str1[i]='\0';
printf("%s", str1);
getch();
}
Related Posts:
- Write a Program in C to Determine Whether a Number is Prime or Not.
- Write a Program in C to Calculate the Factorial Value of an Integer
- C Program to Read Set of Real Numbers from Keyboard & Find the Maximum
- How to Create a Digital Clock in JavaScript?
- How to Detect Visitor's Browser Using JavaScript?
- How to Create JavaScript Image Slideshow with Links
- Simple JavaScript Fade Effect Animation Using Jquery
- How to Create Simple JavaScript Fade Effect Animation?
- How to Create LOV in Oracle Forms Using Wizard ?
- How to Create Oracle Forms by Using Wizard ?
- How To Make Simple CSS Stylesheet for a Website ?
- How To Create Simple Menu Using CSS ?
- How to write a program in C using for Loop?