C言語の例 - 文字列コピー
ある変数の文字列を別の変数にコピーします。
例 - strcpy() を使用
#include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[100];
memset(dest, '\0', sizeof(dest));
strcpy(src, "This is example.com");
strcpy(dest, src);
printf("最終的なターゲット文字列: %s\n", dest);
return(0);
}
出力結果は次のとおりです。
最终的目标字符串: This is example.com
例 - strcpy() を使用しない
#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf("文字列 s1:");
scanf("%s",s1);
for(i = 0; s1[i] != '\0'; ++i)
{
s2[i] = s1[i];
}
s2[i] = '\0';
printf("文字列 s2: %s", s2);
return 0;
}
出力結果は次のとおりです。
字符串 s1: サンプル 字符串 s2: サンプルその他の拡張
C言語の例