Copy string in C without using strcpy
#include <stdio.h>
#include <string.h>
int main()
{
char source[1000], destination[1000];
int c=0;
printf("Input a string\n");
gets(source);
while (source[c] != '\0')
{
destination[c] = source[c];
c++;
}
destination[c] = '\0';
printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
return 0;
}
Creating a function to copy a string
#include <stdio.h>
#include <string.h>
void copy_string(char [], char []);
int main()
{
char source[1000], destination[1000];
printf("Input a string\n");
gets(source);
printf("Source string: %s\n", source);
copy_string(destination, source); /*Function call*/
printf("Destination string: %s\n", destination);
return 0;
}
void copy_string(char destination[], char source[])/*Function Defination*/
{
int c=0;
while (source[c] != '\0')
{
destination[c] = source[c];
c++;
}
destination[c] = '\0';
}
#include <string.h>
void copy_string(char [], char []);
int main()
{
char source[1000], destination[1000];
printf("Input a string\n");
gets(source);
printf("Source string: %s\n", source);
copy_string(destination, source); /*Function call*/
printf("Destination string: %s\n", destination);
return 0;
}
void copy_string(char destination[], char source[])/*Function Defination*/
{
int c=0;
while (source[c] != '\0')
{
destination[c] = source[c];
c++;
}
destination[c] = '\0';
}
Run:
No comments:
Post a Comment