C program to copy a string
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char source[] = "C program";
char destination[50];
clrscr();
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
getch();
}
c program to copy a string using pointers
#include<stdio.h>
#include<conio.h>
#include<string.h>
void copy_string(char*, char*);
void main()
{
char source[100], target[100];
clrscr();
printf("Enter source string\n");
gets(source);
copy_string(target, source);
printf("Target string is \"%s\"\n", target);
getch();
}
void copy_string(char *target, char *source)
{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = '\0';
}
No comments:
Post a Comment