1、用C語言實現字符數組1拷貝到字符數組2,替代strcpy函數

剛入IT行業,還有很多不懂,想深入學習C,一點一點的積累,這個要求曾經在筆試的時候碰到過。題目主要參考尹成老師博客裏面的,拿過來用我自己的思維寫一遍。

#include <stdio.h>
#include <stdlib.h>
#define NUM   100 //給需要拷貝到的數組設定大小
void copy(char str1[], char str2[])
{
int n = 0;
while (str1[n] != '\0')//截取有效字符段
n++;
for (int i = 0; i <= n; i++)
{
str2[i] = str1[i];
printf(" %c", str2[i]);
}
}
void main()
{
char *s1 = "abcdef  ghijklm    nopqrstuvwxyz";//需要拷貝的字符數組
char s2[NUM] = { 0 };
copy(s1, s2);
getchar();
}

運行結果如下:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章