strcpy.c
K&R, p. 105-106
download
#include <stdio.h> // for printf(), scanf()
#define SIZE 100 // array size
void strcpy1(char*, char*);
void strcpy2(char*, char*);
int main()
{
char s[SIZE], t[SIZE];
printf("Type a word: ");
scanf("%s", t);
strcpy1(s, t);
printf("strcpy1(): %s\n", s);
strcpy2(s, t);
printf("strcpy2(): %s\n", s);
return 0;
}
void strcpy1(char *s, char *t) // copy t to s
{
int i = 0;
while (s[i] = t[i]) // while ((s[i] = t[i]) != '\0')
{i++;}
}
void strcpy2(char *s, char *t) // copy t to s
{
while (*s++ = *t++) // while ((*s++ = *t++) != '\0')
{}
}
/*
gcc strcpy.c -o strcpy
./strcpy
Type a word: abracadabra
strcpy1(): abracadabra
strcpy2(): abracadabra
*/
Comments
Post a Comment