2017年3月9日 星期四

strcpy的使用:複製字串內容到另一個字串的方法

程式範例1

#include <stdio.h>
#include <stdlib.h>

int main()
{
      char string1[10];
      char string2[10];

      string1[0] = 'H';
      string1[1] = 'e';
      string1[2] = 'l';
      string1[3] = 'l';
      string1[4] = 'o';
      string1[5] = '!';
      string1[6] = '\0';

      strcpy(string2, string1);  //strcpy 是把右邊字串的內容(string1)複製給左邊的字串(string2)
      printf("string1 = %s \n", string1);
      printf("string2 = %s \n", string2);
      system("pause");
      return 0;
}

程式範例2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char string[10];

    strcpy(string, "Hello everyone! Welcome to my blog!");  //strcpy 是把右邊字串的內容複製給左邊的字串(string)
    printf("string = %s \n", string);
    system("pause");
    return 0;
}

沒有留言:

張貼留言