21 Şubat 2014 Cuma

string copying in c programming


This program copy string using library function strcpy, to copy string without using strcpy see source code below in which we have made our own function to copy string.

C program to copy a string

#include<stdio.h>
#include<string.h>
 
main()
{
   char source[] = "C program";
   char destination[50];
 
   strcpy(destination, source);
 
   printf("Source string: %s\n", source);
   printf("Destination string: %s\n", destination);
 
   return 0;
}

c program to copy a string using pointers

: here we copy string without using strcmp by creating our own function which uses pointers.
#include<stdio.h>
 
void copy_string(char*, char*);
 
main()
{
    char source[100], target[100];
 
    printf("Enter source string\n");
    gets(source);
 
    copy_string(target, source);
 
    printf("Target string is \"%s\"\n", target);
 
    return 0;
}
 
void copy_string(char *target, char *source)
{
   while(*source)
   {
      *target = *source;
      source++;
      target++;
   }
   *target = '\0';
}

Hiç yorum yok:

Yorum Gönder