Tuesday 8 July 2014

Write a Program in C to Copy a String to Another.


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



Program in C to Copy a String to Another



Steps:
  1. At first  declare two strings str1 and str2 and integers m, i, flag and j under main function.
  2. Prompt message to enter the strings and allow to enter strings using gets() function.
  3. Prompt the message and allow to enter the index where you want to insert in the first string.
  4. Copy second string to the first string using while() function.
  5. 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:


No comments:

Post a Comment

Powered by Blogger.

مساحات للبيع

اعلان

معلومات عنا

إعلانات

إجمالي مرات مشاهدة الصفحة

Search This Blog

اخبار عالمية

مقالات

أخبار

أخر المعلقين

فيديوهات

سلايدر

Tags

Contact us

متتبعون المدونة

Categories

Advertisement

40% Off

Translate

صور متنوعة

Facebook

Facebook users

Recent in Mens

FlatBook

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum rhoncus vehicula tortor, vel cursus elit. Donec nec nisl felis. Pellentesque ultrices sem sit amet eros interdum, id elementum nisi ermentum.Vestibulum rhoncus vehicula tortor, vel cursus elit. Donec nec nisl felis. Pellentesque ultrices sem sit amet eros interdum, id elementum nisi fermentum.




Comments

Contact Us

Name

Email *

Message *