20131127【C言語】独自のstrcpyを作成する
お題
独自にstrcpyを作成する
プログラム概要
main処理から文字列をコピーする独自の関数を呼び出す。
その関数の返り値であるコピー先のchar型のポインタを受け取り、
コピー元とコピー先の文字列の値を表示する
ソース
#include <stdio.h>
char *mystrcpy (char *target, const char *source);
int main(void) {
char *p, str[80];
p = mystrcpy(str, "hoge");
printf("%s %s.\n", str, p);
return 0;
}
char *mystrcpy(char *target, const char *source)
{
char *temp;
temp = target;
while(*source)
*target++ = *source++;
/* add null end char */
*target = '\0';
return temp;
}
実行結果
hoge hoge.