多處 `if (s[0] = '0')` **賦值代替比較**,導致條件永真/假,邏輯錯誤。應為 `if (s[0] == '0')` 或 `if (s[0] == '\0')` 判空。
// 輸出串
void output(char *s) {
if (s[0] == '\0') // 判空
cout << "該串為空串,請重新輸入!" << endl;
else
cout << s << endl;
}
// 計算長度
int length(char s[]) {
int i = 0;
if (s[0] == '\0') { // 修正比較
cout << "該串為空串,請重新輸入!" << endl;
return 0;
}
while (s[i] != '\0') i++;
cout << "該串長為:" << i << endl;
return i;
}
#include
#include
string s, t;
// 插入: s.insert(pos, t);
// 刪除: s.erase(pos, len);
// 替換: s.replace(pos, len, t);
// 查找: size_t pos = s.find(t);
// 子串: string sub = s.substr(pos, len);
// 連接: s += t;
**教學階段理解數組實現原理即可,實際開發請直接用 `std::string`**,安全、高效、功能全。
菜單 `switch` 每個 `case` 結束加 `system("cls")` (Windows) 或 `system("clear")` (Linux/Mac) 清屏,體驗更好。