C++ HW6-1 for WallPower

編寫一個C++程式,其中自訂一個函式Mystrcp(),利用指標傳入兩字串起始位置
(兩字串為"I like C" 、 "This is fun")。將字串1複製到字串2,然後印出字串2。
參考輸出如下。


參考答案。
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void Mystrcpy(char *str1, char *str2)
  5. {
  6. int check = 0;
  7. for (int i = 0; *(str2 + i) != '\0'; i++)
  8. {
  9. if (*(str1 + i) == '\0')
  10. check = 1;
  11. if (check == 0)
  12. *(str2 + i) = *(str1 + i);
  13. else
  14. *(str2 + i) = '\0';
  15. }
  16. }
  17.  
  18. int main()
  19. {
  20. char word1[] = "I like C", word2[] = "This is fun";
  21. Mystrcpy(word1, word2);
  22. cout << "word2 = " << word2 << endl;
  23. system("pause");
  24. return 0;
  25. }

留言

這個網誌中的熱門文章

C# 井字遊戲