C++ HW6-1 for WallPower
編寫一個C++程式,其中自訂一個函式Mystrcp(),利用指標傳入兩字串起始位置
(兩字串為"I like C" 、 "This is fun")。將字串1複製到字串2,然後印出字串2。
參考輸出如下。
參考答案。
(兩字串為"I like C" 、 "This is fun")。將字串1複製到字串2,然後印出字串2。
參考輸出如下。
參考答案。
- #include <iostream>
- using namespace std;
- void Mystrcpy(char *str1, char *str2)
- {
- int check = 0;
- for (int i = 0; *(str2 + i) != '\0'; i++)
- {
- if (*(str1 + i) == '\0')
- check = 1;
- if (check == 0)
- *(str2 + i) = *(str1 + i);
- else
- *(str2 + i) = '\0';
- }
- }
- int main()
- {
- char word1[] = "I like C", word2[] = "This is fun";
- Mystrcpy(word1, word2);
- cout << "word2 = " << word2 << endl;
- system("pause");
- return 0;
- }
留言
張貼留言